【问题标题】:mysqli image blob insert not workingmysqli图像blob插入不起作用
【发布时间】:2013-11-10 03:55:40
【问题描述】:

我正在尝试将图像文件上传到我的 MySql 数据库。我已经在网上搜索了,我终于开始使用以下脚本:

if(is_uploaded_file($_FILES['filename']['tmp_name'])){

$maxsize=$_POST['MAX_FILE_SIZE'];       
$size=$_FILES['filename']['size'];

// getting the image info..
$imgdetails = getimagesize($_FILES['filename']['tmp_name']);
$mime_type = $imgdetails['mime']; 

// checking for valid image type
if(($mime_type=='image/jpeg')||($mime_type=='image/gif')||($mime_type=='image/png')){
  // checking for size again
  if($size<$maxsize){
    $filename=$_FILES['filename']['name'];  
    $imgData =addslashes (file_get_contents($_FILES['filename']['tmp_name']));

         if(is_uploaded_file($_FILES['filename']['tmp_name'])){
             debug('subida ok');
         }else{
             debug('fallo subida');
         }

        //echo $imgData;
        $imgDetail=addslashes($imgdetails[3]);


        $db = new mysqli('127.0.0.1','user','pass', 'db');
        if($db->connect_error){
            debug('Error en la conexion : '.$db->connect_errno.
                                       '-'.$db->connect_error);
        }

        $stmt = $db->stmt_init();
        $stmt->prepare("INSERT INTO `image` (`name`, `image`, `type`, `size`) VALUES (?, ?, ?, ?)");
        if($stmt===false){ debug('Error en prepare');}
        $rc=$stmt->bind_param('sbss', $filename, $imgData, $mime_type,$imgDetail);
        if($rc===false){ debug('Error en bind');}
        if($stmt->execute()==false){ debug('error ' . $stmt->error); };
        $stmt->close();


  }else{
    debug("<font class='error'>Image to be uploaded is too large..Error uploading the image!!</font>");
  }
}else{
  debug("<font class='error'>Not a valid image file! Please upload jpeg or gif image.</font>");
}

}else{          
  switch($_FILES['filename']['error']){
case 0: //no error; possible file attack!
  debug("<font class='error'>There was a problem with your upload.</font>");
  break;
case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
  debug( "<font class='error'>The file you are trying to upload is too big.</font>");
  break;
case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
  debug( "<font class='error'>The file you are trying to upload is too big.</font>");
  break;
case 3: //uploaded file was only partially uploaded
  debug( "<font class='error'>The file you are trying upload was only partially uploaded.</font>");
  break;
case 4: //no file was uploaded
  debug( "<font class='error'>You must select an image for upload.</font>");
  break;
default: //a default error, just in case! 
  debug( "<font class='error'>There was a problem with your upload.</font>");
  break;
  }     
}   

上传的图像似乎在照顾脚本输出,但查看数据库大小为 0 字节... 有人知道这是为什么吗?

【问题讨论】:

  • 你的 max_allowed_pa​​cket 是多少?选择@@max_allowed_pa​​cket;
  • 最大允许数据包:1048576
  • 好的,除此之外还有其他问题,您可能无法上传大于 1MB 的图像。在这种情况下,您的图像有多大?它是否适用于较小的东西,例如 1k 测试图像?
  • 12.7k 是当前大小

标签: php insert mysqli blob


【解决方案1】:

mysqli SEND_LONG_DATA()!!!

http://php.net/manual/en/mysqli-stmt.send-long-data.php

对于某些 php/mysql(尤其是 mysqli lib)服务器设置,Blob 似乎特别令人沮丧。在配置设置无法解决问题的情况下,此解决方案似乎始终有效。如果重新实现令人头疼,我会建议 PDO。

【讨论】:

    猜你喜欢
    • 2017-10-20
    • 2013-02-24
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多