Upload size problem in PHP and MySql

2019-03-22 07:46发布

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>

3条回答
小情绪 Triste *
2楼-- · 2019-03-22 08:17

In order to upload large files to your server with PHP, you need to change 2 parameters in your php.ini file.

; Maximum allowed size for uploaded files.
upload_max_filesize = 50M

; Maximum size of POST data that PHP will accept.
post_max_size = 50M

50M = 50Mb

查看更多
Viruses.
3楼-- · 2019-03-22 08:23

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();
查看更多
Summer. ? 凉城
4楼-- · 2019-03-22 08:36
Try these different settings in C:\wamp\bin\apache\apache2.2.8\bin\php.ini

Find:
post_max_size = 8M
upload_max_filesize = 2M
max_execution_time = 30
max_input_time = 60
memory_limit = 8M

Change to:
post_max_size = 750M
upload_max_filesize = 750M
max_execution_time = 5000
max_input_time = 5000
memory_limit = 1000M

Then restart wamp to take effect

Have fun,
查看更多
登录 后发表回答