Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Posts
    14

    Unanswered: image upload to server, writing image name to mysql

    Hello

    I am having problems uploading an image through a HTML form. I want the image to be uploaded to the server and the image name to be written to the mysql database.

    Below is the code I am using:

    Code:
    <?php
    if (isset($_POST['add'])){
       
       echo "<br /> add value is true";
          $name = $_POST['name'];   
          $description = $_POST['description'];   
          $price = $_POST['price'];
          $category_id = $_POST['category_name'];
          $image = $_FILES['image']['name'];   
          
          //file path of the image upload
          $filepath = "../images/";
          //mew name for the image upload
          $newimagename = $name;
          //new width for the image
          $newwidth = 100;
          //new height for the image
          $newheight = 100;
          
          include('../includes/image-upload.php');
          
          
          mysql_query("INSERT INTO item (item_name, item_description, item_price, item_image)
          VALUES ('$name','$description','$price','$image')"); ?>
    Here is the image-upload.php file code:

    Code:
    <?php
    
    //assigns the file to the image
    $image =$_FILES["image"]["name"];
    $uploadedfile =$_FILES["image"]["tmp_name"];
    
    if ($image) {
       //retrieves the extension type from image upload
       $extension = getextension($image);
       //converts extension to lowercase
       $extension = strtolower($extension);
       
       //create image from uploaded file type
       if($extension=="jpg" || $extension=="jpeg") {
          $uploadedfile = $_FILES['image']['tmp_name'];
          $src = imagecreatefromjpeg($uploadedfile);
       }else if($extension=="png") {
          $uploadedfile = $_FILES['image']['tmp_name'];
          $src = imagecreatefrompng($uploadedfile);
       }else{
          $src = imagecreatefromgif($uploadedfile);
       }
       
       //creates a list of the width and height of the image
       list($width,$height)=getimagesize($uploadedfile);
       
       //adds color to the image
       $tmp = imagecreatetruecolor($newwidth,$newheight);
       
       //create image
       imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
       
       //set file name
       $filename = $filepath.$newimagename.".".$extension;
       $imagename = $newimagename.".".$extension;
       //uploads new file with name to the chosen directory
       imagejpeg($tmp,$filename,100);
       
       //empty variables
       imagedestroy($src);
       imagedestroy($tmp);
       
    }
    
    
    ?>
    Any help would be appreciated, fairly new to all this!

    Thanks!!!

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by gwg View Post
    Any help would be appreciated, fairly new to all this!
    i guess a good place to start would be this --
    mysql <> php
    i've flagged your post to request that it be moved to the php forum

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2012
    Posts
    14
    take it you dont know the answer then?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by gwg View Post
    take it you dont know the answer then?
    you catch on quick

    i'm pretty good with mysql, though -- which is the forum you posted in

    but the php might as well be chinese
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Apr 2012
    Posts
    1
    Getting the name of the file is pretty easy. Just do something like
    PHP Code:
    $name $_FILES['image']['name'
    Then just place $name in your DB field.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •