【问题标题】:PHP Can't download over 40MB weight filesPHP 无法下载超过 40MB 的重量文件
【发布时间】:2020-04-07 17:24:02
【问题描述】:

我创建了一个简单的文件来下载文件。 我有以下代码:

function DownloadFile($file) { // $file = include path
    if(file_exists($file)) {
        header('HTTP/1.0 200 OK', true, 200);
        header('Content-Description: File Transfer');
        header('Content-Type: application/zip');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');

        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}

问题是,如果我尝试下载超过 40-50MB 的文件,则下载的文件为空白 (0KB),但如果我尝试低于 40MB,则它可以正常工作。

有什么问题?

【问题讨论】:

  • 这可能与使用 php 输出缓冲区有关。为什么要设置它?
  • 你有同样的问题,只是下载文件的最小 php 文件?
  • Peirre 因为如果没有设置,我会被拒绝下载(ERR_INVALID_RESPONSE)。不,如果我尝试下载 40MB 以下的文件,我不会遇到同样的问题
  • 不明白为什么如果输出缓冲关闭,下载会被拒绝......永远不要打开它,从来没有任何问题
  • 我正在使用 wordpress

标签: php wordpress


【解决方案1】:

就像这篇文章中解释的https://www.sitepoint.com/community/t/php-file-size-download-limit/6541你需要设置

memory_limit = 128M
post_max_size = 300M

在 php.ini 文件中

【讨论】:

    【解决方案2】:

    解决办法:

    function DownloadFile($file) { // $file = include path
        if(file_exists($file)) {
            header('HTTP/1.0 200 OK', true, 200);
            header('Content-Description: File Transfer');
            header('Content-Type: application/zip');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: chunked');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
    
                   ob_clean();
                   flush();
                   ob_end_flush();
    
                if ($fd = fopen ($file, "r")) {
    
                    set_time_limit(0);
                    ini_set('memory_limit', '1024M');
    
                    while(!feof($fd)) {
                        echo fread($fd, 4096);
                        flush();
                    }   
    
                }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 2015-01-12
      • 2022-01-17
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      相关资源
      最近更新 更多