Jump to content

PHP image upload not working...


pritam79

Recommended Posts

Hi everyone I have a HP script that uploads an image file to the server. I am doing it in the WAMP server locally. It has three scripts as follows-This script is used to connect to MySQL and create the database

<?php$db = mysql_connect('localhost', 'root', '') or   die ('Unable to connect. Check your connection parameters.');mysql_select_db('moviesite', $db) or die(mysql_error($db));				 //create the images table$query = 'CREATE TABLE images   (	   image_id	   INTEGER	  NOT NULL AUTO_INCREMENT,	   image_caption  VARCHAR(255) NOT NULL,	   image_username VARCHAR(255) NOT NULL,	   image_filename VARCHAR(255) NOT NULL DEFAULT "",	   image_date	 DATE		 NOT NULL,	   PRIMARY KEY (image_id)   )   ENGINE=MyISAM';mysql_query($query, $db) or die (mysql_error($db));				 echo 'Images table successfully created.';?>

This html file is used to select the image to be uploaded-

<html><head> <title>Upload your pic to our site!</title> <style type="text/css" > <!--td {vertical-align: top;}--> </style></head><body>  <form action="check_image.php" method="post" enctype="multipart/form-data">  <table>   <tr>	<td> Your Username </td>	<td><input type="text" name="username"></td>   </tr>   <tr>	<td> Upload Image* </td>	<td><input type="file" name="uploadfile"></td>   </tr><tr>	<td colspan="2" >	 <small><em>* Acceptable image formats include: GIF, JPG/JPEG and PNG.	  </em></small>	</td>   </tr> <tr>	<td> Image Caption <br>	</td>	<td><input type="text" name="caption"></td>   </tr><tr>	<td colspan="2" style="text-align: center">	 <input type="submit" name="submit" value="Upload">	</td>   </tr>  </table> </form></body></html>

And this script is used to do the uploading part-

<?php$db = mysql_connect('localhost', 'root', '') or   die ('Unable to connect. Check your connection parameters.');mysql_select_db('moviesite', $db) or die(mysql_error($db));				 //change this path to match your images directory$dir ='C:/wamp/www/examples/Chapter-7/images';				 //make sure the uploaded file transfer was successfulif ($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)  // if file was not successfully uploaded{   switch ($_FILES['uploadfile']['error'])	{   case UPLOAD_ERR_INI_SIZE:	   die('The uploaded file exceeds the upload_max_filesize directive in php.ini.');	   break;   case UPLOAD_ERR_FORM_SIZE:	   die('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.');	   break;   case UPLOAD_ERR_PARTIAL:	   die('The uploaded file was only partially uploaded.');	   break;   case UPLOAD_ERR_NO_FILE:	   die('No file was uploaded.');	   break;   case UPLOAD_ERR_NO_TMP_DIR:	   die('The server is missing a temporary folder.');	   break;   case UPLOAD_ERR_CANT_WRITE:	   die('The server failed to write the uploaded file to disk.');	   break;   case UPLOAD_ERR_EXTENSION:	   die('File upload stopped by extension.');	   break;   }}//get info about the image being uploaded$image_caption = $_POST['caption'];$image_username = $_POST['username'];$image_date = date('Y-m-d');list($width, $height, $type, $attr) = getimagesize($_FILES['uploadfile']['tmp_name']);				 // make sure the uploaded file is really a supported imageswitch ($type){case IMAGETYPE_GIF:   $image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or	   die('The file you uploaded was not a supported filetype.');   $ext = '.gif';   break;case IMAGETYPE_JPEG:   $image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or	   die('The file you uploaded was not a supported filetype.');   $ext = '.jpg';   break;case IMAGETYPE_PNG:   $image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or	   die('The file you uploaded was not a supported filetype.');	   $ext = '.png';   break;default:   die('The file you uploaded was not a supported filetype.');}				 //insert information into image table$query = 'INSERT INTO images   (image_caption, image_username, image_date)		 VALUES  ("' . $image_caption . '", "' . $image_username . '", "' . $image_date . '")';$result = mysql_query($query, $db) or die (mysql_error($db));				 //retrieve the image_id that MySQL generated automatically when we inserted the new record$last_id = mysql_insert_id();				 //because the id is unique, we can use it as the image name as well to make sure we don't overwrite another image that already exists$imagename = $last_id . $ext;				 // update the image table now that the final filename is known.$query = 'UPDATE images   SET image_filename = "' . $imagename . '"   WHERE image_id = ' . $last_id;$result = mysql_query($query, $db) or die (mysql_error($db));				 //save the image to its final destinationswitch ($type){case IMAGETYPE_GIF:   imagegif($image, $dir . '/' . $imagename);   break;	case IMAGETYPE_JPEG:   imagejpeg($image, $dir . '/' . $imagename, 100);   break;	case IMAGETYPE_PNG:   imagepng($image, $dir . '/' . $imagename);   break;}	imagedestroy($image);?><html><head> <title>  Here is your pic! </title></head><body> <h1> So how does it feel to be famous?</h1> <p> Here is the picture you just uploaded to our servers:</p>  <img src="images/<?php echo $imagename; ?>  " style="float:left;"> <table>  <tr> <td>  Image Saved as:  </td> <td> <?php echo $imagename; ?> </td> </tr>  <tr> <td>  Image Type:  </td> <td>  <?php echo $ext; ?> </td> </tr>  <tr> <td>  Height:  </td> <td> <?php echo $height; ?> </td> </tr>  <tr> <td>  Width:  </td> <td> <?php echo $width; ?> </td> </tr>  <tr> <td>  Upload Date:  </td> <td> <?php echo $image_date; ?> </td> </tr> </table></body></html>

Now, when I run the 'html' form, the image is selected from any source folder where the image is located. When the "upload" button is then clicked, the image gets uploaded to the "images" folder properly, and the image can be later accessed from the destination folder. but the problem with this script is that after the image is selected and the upload button is clicked the image doesn't appear in the browser whereas the other details of the image like size, dimension etc. get displayed in the browser. Any help plz....

Link to comment
Share on other sites

Use just move_uploaded_file($_FILES['uploadfile']['tmp_name'], $_FILES['uploadfile']['name']);Don't create image etc.From w3schools:

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000))  {  if ($_FILES["file"]["error"] > 0)	{	echo "Return Code: " . $_FILES["file"]["error"] . "<br />";	}  else	{	echo "Upload: " . $_FILES["file"]["name"] . "<br />";	echo "Type: " . $_FILES["file"]["type"] . "<br />";	echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";	echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";	if (file_exists("upload/" . $_FILES["file"]["name"]))	  {	  echo $_FILES["file"]["name"] . " already exists. ";	  }	else	  {	  move_uploaded_file($_FILES["file"]["tmp_name"],	  "upload/" . $_FILES["file"]["name"]);	  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];	  }	}  }else  {  echo "Invalid file";  }?>

Link to comment
Share on other sites

I have now changed only one line in the file "check_image.php", and now I am getting the desired output. Is the change correct or something more proper is required? The changes that were made have been highlighted using comments.

<html><head> <title>  Here is your pic! </title></head><body> <h1> So how does it feel to be famous?</h1> <p> Here is the picture you just uploaded to our servers:</p><!--- this line was the original one  <img src="images\<?php echo $imagename; ?>" style="float:left;"> --><img src="<?php echo $imagename; ?>" style="float:left;"><!-- this is the new line --> <table>  <tr> <td>  Image Saved as:  </td> <td> <?php echo $imagename; ?> </td> </tr>  <tr> <td>  Image Type:  </td> <td>  <?php echo $ext; ?> </td> </tr>  <tr> <td>  Height:  </td> <td> <?php echo $height; ?> </td> </tr>  <tr> <td>  Width:  </td> <td> <?php echo $width; ?> </td> </tr>  <tr> <td>  Upload Date:  </td> <td> <?php echo $image_date; ?> </td> </tr> </table></body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...