BLOB 下载截断为 1 MB 脚本适用于小于 1 MB 的文件
我最近刚刚提出并解决了一个有关将大于 2 MB 的 .PDF 文件作为 BLOBS 上传到 MySQL 数据库的问题。我必须更改 php.ini 文件中的一些设置和 MySQL 的最大数据包设置。然而,解决这个问题让我发现了我的脚本的一个新问题。
现在,由于我可以将文件上传到 BLOB 数据库,因此我尝试下载该文件以进行测试。令我沮丧的是,当我打开 .PDF 文件时,收到以下错误:无法加载文档(错误 3)“file:///tmp/test-13.pdf”。经过进一步调查,我发现正在下载的文件 test.pdf 只有 1 MB,略小于数据库中假定大小(略多于 2 MB)的一半。这显然是错误的原因。
以下代码是我用来从数据库下载文件的脚本的一部分。它位于脚本的最顶部,对于小于 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'];
$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;
}
}
我在想也许我的一些标题语句是错误的?我很困惑该怎么办。我搜索了 php.ini,没有发现任何我认为需要更改的设置,并且我的 MySQL 最大数据包设置是 4 MB,因此应该下载 2 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'];
$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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据(http://dev.mysql.com/doc/refman /5.0/en/blob.html):
根据(http://dev.mysql.com/doc /refman/5.0/en/server-parameters.html) max_allowed_packet 的默认值为 1048576。
According to (http://dev.mysql.com/doc/refman/5.0/en/blob.html):
According to (http://dev.mysql.com/doc/refman/5.0/en/server-parameters.html) the default value for max_allowed_packet is 1048576.
我实际上解决了这个问题。我更改了 php.ini 和 my.cnf 中推荐的所有值,但我还需要更改 PDO 的设置。
我改变了:
PDO::MYSQL_ATTR_MAX_BUFFER_SIZE(整数)
最大缓冲区大小。默认为 1 MiB。
必须在创建 PDO 对象时设置此项才能工作。现在一切都很好。
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.