这个看起来很简单,事实也的确如此。你所要做的就是下载一个文件到你的服务器:

file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));

只有一个问题。如果你有一个大文件,比如100mb。然后,您将耗尽内存,无法下载文件。

我想要的是一种将文件写入磁盘的方法,因为我正在下载它。这样,我就可以下载更大的文件,而不会遇到内存问题。


当前回答

PHP 4和5解决方案:

Readfile()本身不会出现任何内存问题,即使在发送大文件时也是如此。 如果fopen包装器已启用,则URL可以用作此函数的文件名。

http://php.net/manual/en/function.readfile.php

其他回答

private function downloadFile($url, $path)
{
    $newfname = $path;
    $file = fopen ($url, 'rb');
    if ($file) {
        $newf = fopen ($newfname, 'wb');
        if ($newf) {
            while(!feof($file)) {
                fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
            }
        }
    }
    if ($file) {
        fclose($file);
    }
    if ($newf) {
        fclose($newf);
    }
}

最好的解决方案

在系统&中安装aria2c

 echo exec("aria2c \"$url\"")

PHP 4和5解决方案:

Readfile()本身不会出现任何内存问题,即使在发送大文件时也是如此。 如果fopen包装器已启用,则URL可以用作此函数的文件名。

http://php.net/manual/en/function.readfile.php

简单的解决方案:

<?php 
exec('wget http://someurl/file.zip');
set_time_limit(0); 
$file = file_get_contents('path of your file');
file_put_contents('file.ext', $file);