我想通过php将图像存储在mysql数据库中作为blob类型

时间:2011-12-10 23:53:27

标签: php mysql

我想通过php将图像存储在mysql数据库中作为blob类型,但显示以下错误:

  

警告:getimagesize(3272)[function.getimagesize]:无法打开   stream:F:\ XAMPP \ htdocs \ 0412 \ form.php中没有此类文件或目录   第15行

我使用以下代码:

  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 个答案:

答案 0 :(得分:2)

这一行:

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

需要:

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

相反。您正在绊倒getimagesize()从图像数据本身获取图像的大小这一事实。您传递给它的只是一个数字,表示它的上传大小(以字节为单位)。

上面的正确例子打开了图片的临时位置,该位置保存在tmp_name

答案 1 :(得分:1)

只需使用以下代码::

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

}