user2015
user2015

Reputation: 77

I want to store image on mysql database as blob type by php

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";    
        }

        } 

Upvotes: 0

Views: 740

Answers (2)

sabbir
sabbir

Reputation: 37

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";    

} 

Upvotes: 1

Bojangles
Bojangles

Reputation: 101543

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.

Upvotes: 2

Related Questions