0

I would like to ask you how can I properly stored an image both in mysql and in a folder at the moment with this code:

<?php

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

          // Temporary file name stored on the server
          $tmpName  = $_FILES['image']['tmp_name'];  

          // Read the file 
          $fp     = fopen($tmpName, 'r');
          $data = fread($fp, filesize($tmpName));
          $data = addslashes($data);
          fclose($fp);


          // Create the query and insert
          // into our database.
          $query = "INSERT INTO tbl_images ";
          $query .= "(images) VALUES ('$data')";
          $results = mysql_query($query, $conn);

          // Print results
          print "Thank you, your file has been uploaded.";

}
else {
   print "No image selected/uploaded";
}


if (!mysql_query($sql, $link))
   {
   die('Error: ' . mysql_error());
   }

?>'

Also how properly to display it. My database for the images is id, images. Do I need anything more for the job or that is enough. Thank you.

2
  • why not check if($results) print "Thank you, your file has been uploaded."; Commented Dec 4, 2012 at 6:45
  • try.. just store the image path in database and image in server ... and just fetch the image path from database and show it Commented Dec 4, 2012 at 6:48

2 Answers 2

1

From the above code, image will be saved in database not in folder. To save in folder you have to use a below code after insert query.

move_uploaded_file($tmpName,"upload/" .$filename );

here upload is folder name.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried this move_uploaded_file($tmpName,"upload/" .$filename ); but it tells me the following errors: Notice: Undefined variable: filename Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\wamp\tmp\php2EC4.tmp' to 'D:/wamp/www/2ro-2012-2013/upload/'
here $filename is $tmpName.
So i have to say move_uploaded_file($tmpName,"upload/" .$tmpName); or i got it wrong ?
0

If you want to store image in database you can use base64_encode method

$image = file_get_contents('filename.gif');
$imencoded = base64_encode($image);   
$query = "INSERT INTO tbl_images ";
$query .= "(images) VALUES ('$imencoded')";

But storing image in db is not recommended and it will not support in IE6 & 7.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.