关于php:从URL下载文件到服务器

Download File to server from URL

嗯,这个看起来很简单,确实如此。要将文件下载到服务器,只需执行以下操作:

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

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

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


自从PHP 5.1.0,file_put_contents()以来,通过一条流过的手,如$data参数写入零件:

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

From the Manual:

If data [that is the second argument] is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using
stream_copy_to_stream().

谢谢你Hakre


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
    }
}


试着使用曲线

ZZU1

我不确定,但我相信CURLOPT_FILE作为数据脉冲写的,不是缓冲。


  • 在目的地服务器中创建一个叫做"下载"的折页
  • 在目的地服务器中保存文件和运行
  • 下载:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <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>


    Above there is examle(cited by prodigitalson)of code WCHIH not work(reason:missing fopen in curlopt ufile-http://www.webdeveloper.com/forum/showthread.php?(2.68299-resolved-php-script-for-a-cronjob-download-file-unpzck-run-another-php-script)。我无法添加如何使我的点数太低,以至于在下面我提供了一个例子(IT also work for"Local URL"):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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);
        }
    }

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


    有三条路:

  • 文件 U Get U Contents and File U Put U Contents
  • 库尔
  • 福彭
  • 你可以从这里找到例子。


    使用简单方法

    1
    copy($source_url, $local_path_with_file_name);

    注释:如果目的地文件已经存在,它将超过写入。

    复制函数

    Special note:Don't forget to set permission 777 for the destination folder


    我用这个下载文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    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()";
    }

    a PHP 4&Amp 5 solution:

    Readfile()will not present any memory issues,even when sending large files,on its own.一个URL可以用作此函数的文件名,如果FOPEN wrappers已经生成。

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