Upload size problem in PHP and MySql

前端 未结 2 1698
予麋鹿
予麋鹿 2021-01-01 08:06

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 uploa

相关标签:
2条回答
  • 2021-01-01 08:52

    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();
    
    0 讨论(0)
  • 2021-01-01 08:52

    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

    0 讨论(0)
提交回复
热议问题