开发者

BLOB Download Truncated at 1 MB Script Works for Files Less Than 1 MB

I just recently asked and solved a question pertaining to uploading .PDF files that are greater than 2 MB into a MySQL database as BLOBS. I had to change some settings in my php.ini file and MySQLs maximum packet setting. However, fixing this issue has led me to discover a new issue with my script.

Now since I can upload files to my BLOB database I attempted to download the file for testing purposes. Much to my dismay when I went to open the .PDF file I received the following error: Failed to load document (error 3) 'file:///tmp/test-13.pdf'. Upon further investigation I found out that the file being downloaded, test.pdf, was only 1 MB, a little less than half of its supposed size in the database of a little more than 2 MB. This is obviously the reason for the error.

The following piece of code is the part of my script I am using for downloading files from the database. It is is at the very top of of script and works Flawlessly for files that are less than 1 MB.

 foreach($_REQUEST as $key => $value)
 {
 if ($value == 'Open')
   {
    header();
    session_start();
    $dbh = new PDO('mysql:host='.$_SESSION['OpsDBServer'].'.ops.tns.its.psu.edu;  
           dbname='.$_SESSION['OpsDB'], $_SESSION['yoM'], $_SESSION['aMa']);
    $id = $key;
    $sqlDownload = "SELECT name, type, content, size  FROM upload WHERE 
    id='".$id."'";
    $result = $dbh->query($sqlDownload);

    $download = $result->fetchAll();
    $type = $download[0]['type'];
    $size = $download[0]['size'];
    开发者_StackOverflow$name = $download[0]['name'];
    $content = $download[0]['content'];

    header("Content-type: $type");
    header("Content-Disposition: inline; filename=$name");
    header("Content-length: $size");
    header("Cache-Control: maxage=1");
    header("Pragma: public");

    echo $content;

    exit;
   }
 }

I am thinking that maybe I have some header statements wrong? I am very confused about what to do. I have searched through php.ini and I have found no settings that I think need to changed and my maximum packet setting for MySQL is 4 MB so a 2 MB should download.

Thanks for any help.


According to (http://dev.mysql.com/doc/refman/5.0/en/blob.html):

The maximum size of a BLOB or TEXT object is determined by its type, but the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers. You can change the message buffer size by changing the value of the max_allowed_packet variable, but you must do so for both the server and your client program.

According to (http://dev.mysql.com/doc/refman/5.0/en/server-parameters.html) the default value for max_allowed_packet is 1048576.


I actually fixed the issue. I changed all of the values that were recommended here in php.ini and my.cnf but I also needed to change a setting for PDO.

I changed: PDO::MYSQL_ATTR_MAX_BUFFER_SIZE (integer) Maximum buffer size. Defaults to 1 MiB.

This has to be set when the PDO object is created to work though. All is good now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜