在旧MySQL驱动程序中插入BLOB会导致'gone away'错误


Inserting BLOB in legacy MySQL driver results in 'gone away' error

我按照本教程制作了一个图像上传器。它工作得很好,除了当我试图上传更大的文件时:它只是将文件大小"更改"为64KB,只显示大图的一小部分。所以,我谷歌了一下,发现'blob'的最大文件大小为64kb,所以我把它改成了longblob,它的最大文件大小为4GB。

但是现在,试图上传大型图像(1MB和更大),我得到SQL错误'MySQL服务器已经离开'。对于小于1MB的图像,上传可以工作。文件大小为915kb的1400x900图像可以上传,但文件大小为1.6mb的1400x900图像不能上传。一个大小为250x179、文件大小为1mb的gif文件可以上传。似乎如果文件大小大于1MB, PHP中的SQL插入函数就不再连接了:这怎么可能呢?

我的php.ini最大文件大小为32MB,所以这不是问题。我用的是MAMP

HTML:

<form action="tutorial.php" method="POST" enctype="multipart/form-data">
    File:
    <input type="file" name="image" > <input type="submit" value="Upload">
</form>
PHP:

// connect to database
include 'include/config.php'; 
// file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file)){
    echo "Please select an image.";
} else {
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);
    if ($image_size==FALSE){
        echo "This is not an image";    
    }else{
        if(!$insert = mysql_query("INSERT INTO gallery VALUES ('', '$image_name', '$image')")){
            die(mysql_error());
            echo "Problem uploading the image.";
        }else{
            $lastid = mysql_insert_id();
            echo "Image uploaded.<p />Your image:<p /><image src=include/get.php?id=$lastid>";
        }
    }
}
PHP配置:

//database credentials
$username = "root";
$password = "root";
$hostname = "localhost"; 
//connection to the database
mysql_connect($hostname, $username, $password) or die(mysql_error()); 
mysql_select_db('imandra') or die(mysql_error()); 
//echo "Connected to MySQL";
PHP得到:

include 'config.php'; 
$id= addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM gallery WHERE id='$id'");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;

您可能需要查看一下mysql配置文件(my.cnf)中的设置。

增加max_allowed_packet设置的大小,例如

max_allowed_packet=50M