【问题标题】:Physical memory warning物理内存警告
【发布时间】:2018-09-04 03:34:03
【问题描述】:
<?php
file_put_contents("10gb.zip", fopen("http://website.website/10GB.zip", 'r'));
echo "File Downloaded!";

我正在使用此代码将文件从 url 下载到我的服务器。但是当我运行我的代码时,我的软管服务器内存变成了红色! -_- 我的下载卡在 3.79 GB。
下载大文件有什么限制吗?我想下载超过 50 GB 的 5 个进程!有可能吗?

【问题讨论】:

  • 是的,PHP 会尝试加载整个文件内存。您需要拆分文件并处理它们
  • 一个建议,如果你对服务器端有控制权,你可以使用FTP下载文件

标签: php memory warnings hosting shared-hosting


【解决方案1】:

在处理大文件时我会选择流式传输而不是直接复制它们

来自此处提供的示例:http://php.net/manual/en/function.stream-copy-to-stream.php

你可以试试:

<?php   
    function pipe_streams($in, $out)
    {
        while (!feof($in))
            fwrite($out,fread($in,8192));
    }

    pipe_streams("http://website.website/10GB.zip", "10gb.zip");
?>

或使用curl (http://php.net/manual/en/book.curl.php):

<?php
    $url  = "http://website.website/10GB.zip";
    $path = "10gb.zip";

    $fp = fopen($path, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
?>

查看https://www.sitepoint.com/performant-reading-big-files-php/ 了解更多流媒体选项

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-03-04
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
相关资源
最近更新 更多