简体   繁体   中英

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

        } 

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 .

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

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM