从URL下载文件到服务器

385

这个问题似乎很简单,而且确实如此。要将文件下载到您的服务器,您只需要执行以下操作:

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

只有一个问题。如果你有一个大文件,比如100MB。那么,你将会用尽内存,并且无法下载该文件。

我想要的是一种在下载文件时将文件写入磁盘的方法。这样,我就能够下载更大的文件,而不会遇到内存问题。


4
这是设置在您的服务器配置中的,据我所知,PHP无法绕过它(除非直接进行.ini编辑)。 - Ben
12个回答

578
自从PHP 5.1.0以来,file_put_contents()函数支持通过将流句柄作为 $data 参数传递来逐步写入文件内容:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));

从手册中得知:

如果数据(即第二个参数)是一个流资源,则该流的剩余缓冲区将被复制到指定的文件。这类似于使用stream_copy_to_stream()

(感谢Hakre。)


8
那不是我的首选。如果在php.ini中设置了allow_fopen_url Off(出于安全考虑很好),你的脚本将无法正常工作。 - PleaseStand
5
如果是这种情况,我认为file_get_contents()也不起作用(请参见原帖)。 - alex
12
@geoff,我很明确地提到了你想要的功能。也许你想要的是有人为你编写代码 - 但我相信你自己动手也学到了一些东西。此外,如果我们打算评论彼此在SO上的互动 - 请接受更多答案 :) - alex
7
在大多数情况下,使用 fopen 时应该同时使用 'b' 标志,以防止对图像和其他非纯文本文件产生负面影响。 - Wayne Weibel
这种方法比使用 curl 更好吗? - PlayHardGoPro
显示剩余3条评论

141
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);
    }
}

1
谢谢你的代码片段,但是 @xaav 能否解释一下你的代码呢?我对 PHP 不是特别擅长。1024*8 是什么意思?再次感谢。 - vvMINOvv
2
具体来说,它的意思是每次读取最多8KB(每个KB有1024字节),因为参数是以字节为单位的。 只要行数小于或等于8KB,就会一次性读取整行。 - Doktor J
2
为什么这不是最佳答案? - GunJack
1
你如何处理这种方法中的错误?如果返回404或连接中断或超时会怎么样? - Adam Swinden
1
注意:$path 应该包括您想要创建的文件的名称。例如:$path = $_SERVER['DOCUMENT_ROOT'].'/uploads/' . "test.mp3"; - NomanJaved
显示剩余11条评论

74

尝试使用 cURL

set_time_limit(0); // unlimited max execution time
$options = array(
  CURLOPT_FILE    => '/path/to/download/the/file/to.zip',
  CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files
  CURLOPT_URL     => 'http://remoteserver.com/path/to/big/file.zip',
);

$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);

我不确定,但我认为使用CURLOPT_FILE选项时,它会在获取数据的同时进行写入,即不进行缓冲。


2
通常情况下这没问题,但是我在一个Web应用程序中使用这段代码,所以我不能确定用户是否安装了cURL。不过,我已经给它点了赞。 - xaav
@Geoff 这是一个分布式网络应用程序吗?因为如果您控制托管,则与您的用户无关(cURL是服务器上的库)。 - alex
不,我不控制托管。它是一个分布式的Web应用程序,任何人都可以拥有。 - xaav
4
可能会缺少Curl。但是几乎所有的共享托管公司都默认安装了CURL。我的意思是,我还没有见过一个没有安装CURL的。 - Mangirdas Skripka
23
根据我的测试,您不能直接将文件路径分配给CURLOPT_FILE。它必须是文件处理器。首先,使用 $fh = fopen('/path/to/download/the/file/to.zip', 'w'); 打开文件,然后在 curl_close($ch); 之后使用 fclose($fh); 关闭文件。并设置 CURLOPT_FILE => $fh - Guscie

29

prodigitalson的回答对我没用。我得到了missing fopen in CURLOPT_FILE更多细节

这个方法对我有用,包括本地URL:

function downloadUrlToFile($url, $outFileName)
{   
    if(is_file($url)) {
        copy($url, $outFileName); 
    } else {
        $options = array(
          CURLOPT_FILE    => fopen($outFileName, 'w'),
          CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files
          CURLOPT_URL     => $url
        );

        $ch = curl_init();
        curl_setopt_array($ch, $options);
        curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $httpcode;
    }
}

        

20
  1. 在目标服务器上创建一个名为“downloads”的文件夹
  2. 将[此代码]保存到.php文件中并在目标服务器上运行

下载器:

<html>
<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
<?php
    // maximum execution time in seconds
    set_time_limit (24 * 60 * 60);

    if (!isset($_POST['submit'])) die();

    // folder to save downloaded files to. must end with slash
    $destination_folder = 'downloads/';

    $url = $_POST['url'];
    $newfname = $destination_folder . basename($url);

    $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);
    }
?>
</html> 

这假设用户想要一个独立的脚本,而不是在现有的PHP应用程序中运行的解决方案,我相信OP和大多数其他人都在寻找后者。对于想要了解方法的人来说,解释也会很有帮助。 - Sean the Bean
1
每次我尝试这样做时,传输的文件大小总是50816,但我的文件大小比这个大。120MB。有什么想法为什么会这样? - Riffaz Starr
set_time_limit(24 * 60 * 60); 必须放在循环内。在脚本开头没有效果。 - Viktor Joras
如何从此网址下载:https://filehippo.com/download_mozilla-firefox-64/post_download/ - Saurin Dashadia

18
set_time_limit(0); 
$file = file_get_contents('path of your file');
file_put_contents('file.ext', $file);

你的答案非常简单且有效,帮助我在cURL无法获取文件时解决了问题。谢谢 :) - Tommix
2
你可能想解释一下这实际上是做什么的。 - alex
7
这并没有解决原帖作者超出了 PHP 内存限制的问题。 - user9645
这非常简单和直接。对于文件较小或环境为本地开发的简单情况非常有用。 - Valentine Shi
有关 .xlsx 文件的任何想法吗?它存储了一个大小为 0 字节的空文件。 - Dhruv Thakkar

12

使用php中的简单方法copy()

copy($source_url, $local_path_with_file_name);

注意:如果目标文件已经存在,它将被覆盖。

PHP copy() 函数

注意:您需要为目标文件夹设置权限777。在下载到本地计算机时,请使用此方法。

特别提示:777是Unix基础系统中的一种权限,拥有所有者、组和所有人的完全读/写/执行权限。通常我们会给这些资产赋予此权限,这些资产不必在Web服务器上对公众隐藏。例如:images文件夹。


1
我绝对不会在Web服务器上将权限设置为777,而且我会驱逐任何有这种想法的Web开发人员。每时每刻,无论何地都是如此。要小心!你不能这样做!要考虑安全性。遵循OWASP规则是不够的。对简单事物有良好的思考方式也很重要。 - BendaThierry.com
@ThierryB。注意:我已经给出了本地路径。这可以用于内部应用程序。具有良好的阅读和理解问题和答案的能力很重要。考虑不同的情况。这不是接受/最佳答案。每个问题都有不同的答案,其中包含优缺点。举个例子让你理解:即使斐波那契数列有多个独特的解决方案,只有一个是最好的。其他解决方案将在不同的情况下使用。 - Pradeep Kumar
好的,但是花时间思考最佳实践并在安全的地方实施它们将使您更好地理解必须实施的概念。也许,如果入侵者进入您的家中,做一些陷阱或以最佳方式构建东西将会给他带来一些麻烦 ;) - BendaThierry.com

9

有三种方法:

  1. 使用file_get_contents和file_put_contents
  2. CURL
  3. fopen

你可以在这里找到示例。


5
我使用这个来下载文件。
function cURLcheckBasicFunctions()
{
  if( !function_exists("curl_init") &&
      !function_exists("curl_setopt") &&
      !function_exists("curl_exec") &&
      !function_exists("curl_close") ) return false;
  else return true;
}

/*
 * Returns string status information.
 * Can be changed to int or bool return types.
 */
function cURLdownload($url, $file)
{
  if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
  $ch = curl_init();
  if($ch)
  {

    $fp = fopen($file, "w");
    if($fp)
    {
      if( !curl_setopt($ch, CURLOPT_URL, $url) )
      {
        fclose($fp); // to match fopen()
        curl_close($ch); // to match curl_init()
        return "FAIL: curl_setopt(CURLOPT_URL)";
      }
      if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {
        curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
        if( !curl_setopt($ch, CURLOPT_HEADER, $curlopt_header)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
        if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
        if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
        if( !curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects) ) return "FAIL: curl_setopt(CURLOPT_MAXREDIRS)";

        return curl_exec($ch);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
        if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
        if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
        if( !curl_setopt($ch, CURLOPT_HEADER, true)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
        if( !curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)) return "FAIL: curl_setopt(CURLOPT_RETURNTRANSFER)";
        if( !curl_setopt($ch, CURLOPT_FORBID_REUSE, false)) return "FAIL: curl_setopt(CURLOPT_FORBID_REUSE)";
        curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
    }
      // if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
      // if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
      // if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
      if( !curl_exec($ch) ) return "FAIL: curl_exec()";
      curl_close($ch);
      fclose($fp);
      return "SUCCESS: $file [$url]";
    }
    else return "FAIL: fopen()";
  }
  else return "FAIL: curl_init()";
}

如何从此网址下载:https://filehippo.com/download_mozilla-firefox-64/post_download/ - Saurin Dashadia

4

一个适用于PHP 4 & 5的解决方案:

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

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


1
这并没有回答问题,因为问题是关于将数据写入磁盘而不是输出缓冲区的。 - Lorenz Meyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接