从URL下载文件到服务器

xaa*_*aav 324 php http download stream

嗯,这个看起来很简单,而且确实如此.要将文件下载到服务器,您只需:

file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));
Run Code Online (Sandbox Code Playgroud)

只有一个问题.如果您有一个大文件,如100mb,该怎么办?然后,您将耗尽内存,无法下载该文件.

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

ale*_*lex 469

从PHP 5.1.0开始,file_put_contents()支持通过传递stream-handle作为$data参数逐个编写:

file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
Run Code Online (Sandbox Code Playgroud)

从手册:

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

(谢谢哈克.)

  • @geoff我是具体的,我提到了你想要的功能.您可能想要的是为您编写代码的人 - 但我确信您自己学到了一些东西.另外,如果我们要对彼此的SO互动发表评论 - [请接受更多答案](http://blog.stackoverflow.com/2009/08/new-question-asker-features/):) (10认同)
  • 那不是我的第一选择.如果在php.ini中设置了`allow_fopen_url Off`(安全性的好主意),那么你的脚本就会被破坏. (4认同)
  • @idealmachine我认为`file_get_contents()`如果是这样的话也不会起作用(参见OP). (4认同)
  • 在大多数情况下,'fopen`也应该使用'b'标志; 防止对图像和其他非纯文本文件的不利影响. (4认同)

xaa*_*aav 130

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,非常有用! (2认同)
  • 这是要走的路! (2认同)
  • 具体来说,它意味着一次读取最多8KB(1024字节/ KB*8),因为参数以字节为单位.只要行<= 8KB,它就会立即读取整行. (2认同)
  • 为什么这不是最佳答案? (2认同)

pro*_*son 65

尝试使用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);
Run Code Online (Sandbox Code Playgroud)

我不确定,但我相信CURLOPT_FILE它在写入数据时所写的选项,即.没有缓冲.

  • 从我的测试中,您不能直接为CURLOPT_FILE分配文件路径.它必须是文件处理程序.首先,用`$ fh = fopen('/ path/to/download/the/file/to.zip','w')打开文件;`并在`curl_close之后用`fclose($ fh);`关闭$ CH);`.并设置`CURLOPT_FILE => $ fh` (17认同)
  • 卷曲可能会丢失.但是,几乎所有共享托管公司都默认安装了CURL.我的意思是,我还没有看到一个没有的. (3认同)
  • 通常情况下,这很好,但我在Web应用程序中有这个代码,所以我不能确定用户将安装cURL.但是,我确实给了这个投票. (2认同)

Kam*_*ski 20

上面有代码(引用prodigitalson)代码wchih不起作用(原因:在CURLOPT_FILE中缺少fopen - http://www.webdeveloper.com/forum/showthread.php?268299-RESOLVED-PHP-script-for-a- cronjob-download-file-unpzck-run-another-php-script).我不能在那里添加评论因为我的点数太少所以下面我给出了工作示例(它也适用于"本地网址"):

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);
        curl_close($ch);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 19

  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> 
Run Code Online (Sandbox Code Playgroud)


小智 16

set_time_limit(0); 
$file = file_get_contents('path of your file');
file_put_contents('file.ext', $file);
Run Code Online (Sandbox Code Playgroud)

  • 这并未解决OP超出PHP内存限制的问题. (6认同)
  • 您可能想解释一下这实际上是做什么的. (2认同)

小智 8

有3种方式:

  1. file_get_contents和file_put_contents
  2. 卷曲
  3. FOPEN

你可以在这里找到例子.


Pra*_*ran 7

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

copy($source_url, $local_path_with_file_name);
Run Code Online (Sandbox Code Playgroud)

注意:如果目标文件已存在,则将覆盖该文件

PHP copy()函数

特别注意:不要忘记为目标文件夹设置权限777


Hoà*_*gtt 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()";
}
Run Code Online (Sandbox Code Playgroud)