php中的文件下载,内存限制问题?

问题描述 投票:7回答:2

我的一个客户决定将网站从一个不错的服务器移到一个...。让我们称它为次要的服务器。

问题是,有一个40MB的文件要下载,服务器上的内存限制为32。对于我来说,更困难的是,它们不允许fopen ...

此外,如果我将文件大小减小到20MB,则可以正常工作。

所以,我的问题是,除了减小文件大小之外,我还能做什么?]

谢谢

编辑:`

        $fsize = filesize($file_path);
        $path_parts = pathinfo($file_path);
        $ext = strtolower($path_parts["extension"]);


        switch ($ext) {
            case "pdf": $ctype = "application/pdf";
                break;
            case "exe": $ctype = "application/octet-stream";
                break;
            case "zip": $ctype = "application/zip";
                break;
            case "doc": $ctype = "application/msword";
                break;
            case "xls": $ctype = "application/vnd.ms-excel";
                break;
            case "ppt": $ctype = "application/vnd.ms-powerpoint";
                break;
            case "gif": $ctype = "image/gif";
                break;
            case "png": $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg": $ctype = "image/jpg";
                break;
            default: $ctype = "application/force-download";
        }

        header("Pragma: public"); // required
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false); // required for certain browsers
        header("Content-Type: $ctype");
        header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\";");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . $fsize);
        ob_clean();
        flush();
        readfile($file_path);`

我在php.net上看到的代码

// If it's a large file, readfile might not be able to do it in one go, so:
$chunksize = 1 * (1024 * 1024); // how many bytes per chunk
if ($size > $chunksize) {
  $handle = fopen($realpath, 'rb');
  $buffer = '';
  while (!feof($handle)) {
    $buffer = fread($handle, $chunksize);
    echo $buffer;
    ob_flush();
    flush();
  }
  fclose($handle);
} else {
  readfile($realpath);
}

我的一个客户决定将网站从一个不错的服务器迁移到一个...。问题是,有一个40MB的文件要下载,并且内存限制在...

php apache file memory limit
2个回答
3
投票

改为使用readfile()。它将以小块形式流式传输文件并处理所有后台工作,以使内存使用量降至最低。


0
投票

您确实想要readfile(),但是如果主机禁用了fpassthru(),他们也可能也禁用了fpassthru()。在这种情况下,您要么与房东协商,要么开始寻找更好的房东。或者,您根本无法分发大文件。

© www.soinside.com 2019 - 2024. All rights reserved.