上传PHP和MySql的大小问题 [英] Upload size problem in PHP and MySql

查看:115
本文介绍了上传PHP和MySql的大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过PHP将文件上传到MySql数据库。
我可以上传1MB大小的文件(通过反复试验找出)。
大小超过1 MB的文件无法上传。
PHP中mysql_error()函数输出的MySql错误是:
MySQL服务器已经消失了

任何人都可以帮我解决这个问题吗?
MySql服务器启动并运行,只对请求大于1MB的请求发生这个错误。



问候,
Mayank。



PS:
我正在使用表单上传文件。
< FORM METHOD =postACTION =fileUpload.phpENCTYPE =multipart / form-data>
< INPUT TYPE =hiddenNAME =MAX_FILE_SIZEVALUE =300000000>
< INPUT TYPE =hiddenNAME =actionVALUE =upload>
描述:< TEXTAREA NAME =txtDescriptionROWS =1COLS =80>< / TEXTAREA>
< INPUT TYPE =fileNAME =binFileID =binFile>
< INPUT TYPE =submitNAME =上传VALUE =上传>
< / FORM>

解决方案

你的sql查询可能超过 max_allowed_pa​​cket 大小,在这种情况下,服务器将断开连接。
您可能对 mysqli_stmt :: send_long_data 它允许你以块的形式发送比max_allowed_pa​​cket更长的参数。

更新:我怎样才能改变它?使用mysqli是唯一的选择?

Afaik的值不能改变每个会话基础,即如果你不能改变服务器配置(my.cnf或启动参数)的值将是只读的。编辑:正如评论建议,如果你有正确的权限
PDO / PDO_MYSQL(从phpversion 5.3.0开始)似乎不支持send_long_data,但我也不确定。这将使 mysqli 成为唯一的选择。我最近注意到, Wez Furlong 加入了堆栈溢出。因为他是PDO实现的作者之一,他可能知道(尽管他没有写pdo_ mysql 模块)。


未经测试和丑陋)示例

  // $ mysqli = new mysqli(.... 
$ fp = fopen( $ _FILES ['binFile'] ['tmp_name'],'rb')或die('!fopen');

// $ result = $ mysqli-> query('SELECT @@ ($ mysqli-> error);
// $ chunkSize = $ result-> fetch_all();
// $ chunkSize = $ maxsize [0] [0];
$ chunkSize = 262144; // 256k块

$ stmt = $ mysqli-> prepare('INSERT INTO foo(desc,bindata)VALUES(?,?)')or die ($ mysqli-> error);
//无声地截断描述为8k
$ desc = 8192< strlen($ _ POST ['txtDescription'])?$ _POST ['txtDescription']: substr($ _ POST ['txtDescription'],0,8192);
$ stmt-> bind_param('sb',$ desc,null);

while(!feof($ fp)){
$ chunk = fread($ fp,$ chunkSize);
$ stmt-> sen d_long_data(1,$ chunk)或死('!send_long_data。'。$ stmt->错误);
}
$ result = $ stmt-> execute();


I am uploading files to a MySql DB through PHP. I am able to upload files upto 1MB size (found out by trial and error). Files greater than 1 MB in size are not getting uploaded. The MySql error printed by mysql_error() function in PHP is: MySQL server has gone away

Can anybody please help me with this? The MySql server is up and running only for requests > 1MB it is giving this error.

Regards, Mayank.

P.S.: I am using a form to upload the file. <FORM METHOD="post" ACTION="fileUpload.php" ENCTYPE="multipart/form-data"> <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="300000000"> <INPUT TYPE="hidden" NAME="action" VALUE="upload"> Description: <TEXTAREA NAME="txtDescription" ROWS="1" COLS="80"></TEXTAREA> <INPUT TYPE="file" NAME="binFile" ID="binFile"> <INPUT TYPE="submit" NAME="Upload" VALUE="Upload"> </FORM>

解决方案

Your sql query probably exceeds the max_allowed_packet size in which case the server will disconnect.
You might be interested in mysqli_stmt::send_long_data which allows you to send parameters longer than max_allowed_packet in chunks.

Update: "How can i change it? Is using mysqli is the only option?"
Afaik the value can't be altered on a per-session base, i.e. if you cannot change the server configuration (my.cnf or startup parameters) the value will be read-only. edit: As the comment suggests you can change the global value of the mysql server after it has been started if you have the proper permissions. PDO/PDO_MYSQL (as of phpversion 5.3.0) doesn't seem to support send_long_data, but I'm not sure about that either. That would leave mysqli as the only option. I've recently noticed that Wez Furlong joined stack overflow. Since he is one of the authors of the PDO implementation he might know (though he did not write the pdo_mysql module).

(Completely untested and ugly) example

// $mysqli = new mysqli(....
$fp = fopen($_FILES['binFile']['tmp_name'], 'rb') or die('!fopen');

//$result = $mysqli->query('SELECT @@max_allowed_packet') or die($mysqli->error);
//$chunkSize = $result->fetch_all();
//$chunkSize = $maxsize[0][0];
$chunkSize = 262144; // 256k chunks

$stmt = $mysqli->prepare('INSERT INTO foo (desc, bindata) VALUES (?,?)') or die($mysqli->error);
// silently truncate the description to 8k
$desc = 8192 < strlen($_POST['txtDescription']) ? $_POST['txtDescription'] : substr($_POST['txtDescription'], 0, 8192);
$stmt->bind_param('sb', $desc, null);

while(!feof($fp)) {
  $chunk = fread($fp, $chunkSize);
  $stmt->send_long_data(1, $chunk) or die('!send_long_data.'.$stmt->error);
}
$result = $stmt->execute();

这篇关于上传PHP和MySql的大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆