image upload to server, writing image name to mysql

images php coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Fri Mar 30, 2012 3:51 am

image upload to server, writing image name to mysql

Postby greenh » Fri Mar 30, 2012 4:35 am


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: Select all

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;
      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: Select all


//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);
      $src = imagecreatefromgif($uploadedfile);
   //creates a list of the width and height of the image
   //adds color to the image
   $tmp = imagecreatetruecolor($newwidth,$newheight);
   //create image
   //set file name
   $filename = $filepath.$newimagename.".".$extension;
   $imagename = $newimagename.".".$extension;
   //uploads new file with name to the chosen directory
   //empty variables


Any help would be appreciated, fairly new to all this!


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 4 guests