BLOB 下载截断为 1 MB 脚本适用于小于 1 MB 的文件

发布于 2024-08-08 12:03:48 字数 1379 浏览 1 评论 0原文

我最近刚刚提出并解决了一个有关将大于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自我难过 2024-08-15 12:03:48

根据(http://dev.mysql.com/doc/refman /5.0/en/blob.html):

最大尺寸
BLOB 或 TEXT 对象由以下条件确定
它的类型,但是你最大的值
实际上可以在之间传输
客户端和服务器由
可用内存量和
通信缓冲区的大小。
您可以更改消息缓冲区大小
通过改变的值
max_allowed_pa​​cket 变量,但是你
必须为服务器和
您的客户程序。

根据(http://dev.mysql.com/doc /refman/5.0/en/server-parameters.html) max_allowed_pa​​cket 的默认值为 1048576。

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.

时光沙漏 2024-08-15 12:03:48

我实际上解决了这个问题。我更改了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文