0

I want to store image on mysql database by php as blob type, but the following error is shown:

Warning: getimagesize(3272) [function.getimagesize]: failed to open stream: No such file or directory in F:\XAMPP\htdocs\0412\form.php on line 15

I use the following code:

  if($_POST['upload'] == 'upload' ) {
     // connect to database
     mysql_connect("localhost","root","") or die(mysql_error());
     mysql_select_db("image") or die(mysql_error());

     // name of the upload image
     $name = addslashes($_FILES['uploadImage']['name']);
     // image
     $image = addslashes( file_get_contents( $_FILES['uploadImage']['tmp_name']) );
     $size = getimagesize($_FILES['uploadImage']['size']);


     if( $size == FALSE ) {
        echo "NO image selected $form";  
     }
     else {
        move_uploaded_file($_FILES['uploadImage']['tmp_name'],"UploadImage/".$name);

        if( !( $result = mysql_query(" INSERT INTO image VALUES ('','$name','$image') ") ) ) {
           echo "uploading image problem $form";    
        }

        } 
2
  • Why would you want to store an image in a database, anyways? Your filesystem is perfectly fine for doing so. Commented Dec 11, 2011 at 0:04
  • You have asked this question before. People have commented saying you shouldn't store images in a database. I for one strongly urge you to take their advice and store images on disk. Commented Dec 11, 2011 at 0:13

2 Answers 2

2

This line:

$size = getimagesize($_FILES['uploadImage']['size']);

Needs to be:

$size = getimagesize($_FILES['uploadImage']['tmp_name']);

Instead. You're tripping up over the fact that getimagesize() gets the size of the image from the image data itself. All you've passed to it is a number indicating it's upload size in bytes.

The correct example above opens the image from it's temporary location, which is held in tmp_name.

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

2 Comments

Not a problem. As @aefxx says in his comment, it's far better to store your files in your filesystem, not in a database for various reasons. Also, don't forget to accept answers to your questions, present and future.
@N.B. I appreciate your efforts (thank you), but I don't think sabbir will take notice after more than two months :-P
1

Just use the following code ::

if($_POST['upload'] == 'upload' ) {

    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("image") or die(mysql_error());

    $name = addslashes($_FILES['uploadImage']['name']);
    $image = file_get_contents( $_FILES['uploadImage']['tmp_name']) ;

     if( !( $result = mysql_query(" INSERT INTO image VALUES ('','$name','$image')") ) )          echo "uploading image problem $form";    

} 

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.