使用php(版本>;=5.4)将非常大的文件下载到服务器


Downloading very big files to server using php (version >= 5.4)

我必须将php中非常大的文件下载到我的服务器上,以便稍后处理它们。我使用流处理程序(php>=5.1支持)找到了以下解决方案:https://stackoverflow.com/a/3938551/1391074

它的代码:

file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));

问题是:我应该关闭fopen打开的连接吗。还是保持原样?

似乎是的,我们必须在手动完成任务后关闭给定的流:

$filename = '/tmp/' . uniqid();
$h = fopen($url, 'r');
if (!file_put_contents($filename, $h)) {
    throw new Exception('Xml download failed');
}
var_dump($h);         // resource(5) of type (stream)
var_dump(fclose($h)); // bool(true)
var_dump($h);         // resource(5) of type (Unknown)