PHP 强制下载 - 大于 100MB 的文件将仅下载少量内容

发布于 2024-12-27 04:54:07 字数 1468 浏览 0 评论 0原文

在我的托管提供商升级服务器 (Debian) 和 PHP(从 5.2.6 到 5.3.2)后,我的网站上的文件下载脚本出现了问题。小于 100MB 的文件可以正常下载,但大于 100MB 的文件将仅下载为 156 字节文件...这是我的下载脚本:

class Download_Controller extends Website_Controller
{

    public function index()
    {
        if (isset($_GET['file'])) {
          $file     = $_GET['file'];
          $filORM   = ORM::factory('file')->where('filename', $file)->find();

          if ($filORM->loaded and $filORM->deleted=='N' and file_exists(APPPATH.'downloads/'.$file) ) {
            //we can serve file download
            $this->auto_render = false;

            $filORM->counter = $filORM->counter + 1;
            $filORM->save();

            $dl = ORM::factory('download');
            $dl->download_file_id = $filORM->id;
            $dl->created = time();
            $dl->country_id = $this->country->id;
            $dl->ip = $this->_getRealIpAddr();
            $dl->browser = Kohana::user_agent('browser');
            $dl->version = Kohana::user_agent('version');
            $dl->platform = Kohana::user_agent('platform');
            $dl->save();

            return download::force(APPPATH.'downloads/'.$file);
          }
          else {
            $this->download_error();
          }

        }
        else {
            //else here we load download center UI
            $this->section();
        }
    }   
}

我正在使用 Kohana PHP 框架。版本 2.3.x。

After my hosting provider upgraded server (Debian) and PHP (from 5.2.6 to 5.3.2) I am having problems with my file download script on our website. Files smaller then 100MB will download fine, but file larger then 100MB will download only as 156 Bytes file ... Here is my download script:

class Download_Controller extends Website_Controller
{

    public function index()
    {
        if (isset($_GET['file'])) {
          $file     = $_GET['file'];
          $filORM   = ORM::factory('file')->where('filename', $file)->find();

          if ($filORM->loaded and $filORM->deleted=='N' and file_exists(APPPATH.'downloads/'.$file) ) {
            //we can serve file download
            $this->auto_render = false;

            $filORM->counter = $filORM->counter + 1;
            $filORM->save();

            $dl = ORM::factory('download');
            $dl->download_file_id = $filORM->id;
            $dl->created = time();
            $dl->country_id = $this->country->id;
            $dl->ip = $this->_getRealIpAddr();
            $dl->browser = Kohana::user_agent('browser');
            $dl->version = Kohana::user_agent('version');
            $dl->platform = Kohana::user_agent('platform');
            $dl->save();

            return download::force(APPPATH.'downloads/'.$file);
          }
          else {
            $this->download_error();
          }

        }
        else {
            //else here we load download center UI
            $this->section();
        }
    }   
}

I am using Kohana PHP framework. Version 2.3.x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

俯瞰星空 2025-01-03 04:54:07

在评论中,您给了我示例链接,我尝试了一个,我下载的 156 字节文件包含以下内容:

致命错误:第 93 行 /home/www-data/system/helpers/download.php 中允许的内存大小 134217728 字节已耗尽(尝试分配 141637633 字节)

很明显 - PHP 内存不足。我猜想在升级时他们还更改了 php.ini 中的内存限制。短期解决方案是将其改回原始(更高)值。

要下载大文件,您应该查看 mod_xsendfile (也可用于 apache 以外的服务器),这涉及设置特殊的 http 标头,并将工作留给网络服务器而不是 php。

In the comments, you gave me example links, I tried one, and that 156-byte file I downloaded contained this:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 141637633 bytes) in /home/www-data/system/helpers/download.php on line 93

It's quite clear - PHP ran out of memory. I presume while upgrading they also changed the memory_limit in php.ini. Short-term solution is to change it back to it's original (higher) value.

For downloading large files, you should look into mod_xsendfile (also available for servers other than apache), that involves setting a special http header, and leaving the work to the webserver instead of php.

拒绝两难 2025-01-03 04:54:07

如果 Kohana 的 download::force() 的工作方式与任何其他框架中的工作方式相同 - PHP 根本不能或不允许在内存中保存超过 100MB 的数据。

If Kohana's download::force() works the same way as in probably any other framework - PHP simply cannot or isn't allowed to hold more than 100MB of data in memory.

失与倦" 2025-01-03 04:54:07

我不知道 download::force() 的代码是什么,但我认为它将整个文件加载到内存中,并且 PHP 停止执行,并出现类似 Allowed memory size is exeded 的错误。您需要按小块加载和输出文件,检查客户端是否中止连接。

更新

您的文件包含致命错误:第 93 行 /home/www-data/system/helpers/download.php 中允许的内存大小 134217728 字节耗尽(尝试分配 141637633 字节)< /代码>。因此,正如我所写,将其分成小块输出。

I don`t know what`s the code of download::force(), but I think that it loads entire file into memory and PHP stops executing with error like Allowed memory size is exhausted. You need to load and output your file by small chunks checking if client aborted connection.

Update

Your file contains Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 141637633 bytes) in /home/www-data/system/helpers/download.php on line 93. So, as I wrote, output it by small chunks.

一张白纸 2025-01-03 04:54:07

你可以尝试直接readfile(APPPATH.'downloads/'.$file)然后exit()而不return,那么你就不会不再受内存问题的束缚

You could try to readfile(APPPATH.'downloads/'.$file) and then exit() directly without return, then you will not be bound to memory issues anymore

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