【问题标题】:How to download part of a file via FTP in PHP?如何在 PHP 中通过 FTP 下载文件的一部分?
【发布时间】:2010-02-10 17:23:06
【问题描述】:

我想在 FTP 服务器上下载部分文件。我有这个解决方案:

  $opts = array('ftp'=>array('overwrite'=>false, 'resume_pos'=> 5*16+12));      
  $context = stream_context_create($opts);

  $version = file_get_contents
    (
     'ftp://'.$ftpAccount["username"].':'.$ftpAccount["password"].'@'.$ftpAccount["server"].'/firm/'.$file, FILE_BINARY, $context, -1, 20
    );

我不喜欢这个解决方案,因为它会为每个文件打开新连接。有谁知道更好的解决方案(有效的)?

【问题讨论】:

    标签: php ftp download


    【解决方案1】:

    这是一个关于 curl 库的类似问题。 http://curl.haxx.se/mail/lib-2005-01/0176.html

    看起来你不能重用 ftp 的连接,不像 http。

    【讨论】:

      【解决方案2】:

      我在自己寻找此问题的解决方案时遇到了此页面。我什么也没找到,所以我自己编造了一些东西。

      可能不是最漂亮的解决方案,但这对我有用:

      它以 512000 字节的块下载远程文件。下载所有块后,它会将本地文件放在一起并删除这些块。它总是覆盖本地文件(实际上它会在开始向其写入块之前删除本地文件)。

      我希望这对某人有所帮助。

      我想如果我愿意让它更干净,我也可以使用 curl 来确定远程文件的大小。但我更像是“胶带程序员”。它以这种方式工作,这对我来说很重要:-)

      $server = '...'; //for example ftp.testftp.nl
      $full_remoteURI = '...'; // for example ftp://ftp.testftp.nl/exports/dump.xml
      $username = '...';
      $passwrd = '...';
      $remote_ftp_path = '...'; // for exmple /exports
      $remote_filename = '...';
      $local_filename = '...';
      
          $ftpconnect = ftp_connect($server);
          if($ftpconnect) $login = ftp_login($ftpconnect, $username, $passwrd);
          else print "<p>FTP verbinding mislukt.</p>\n";
          if($login) {
          ftp_chdir($ftpconnect, $remote_ftp_path);
          $fsize = ftp_size($ftpconnect, $remote_filename);
              if(isset($_GET['f'])) $f = $_GET['f'];
              else $f = 0;
              if($f > 0) $start = ($f * 512001);
              else $start = 0;
              if($start < $fsize) {
              $curl = curl_init();
              $file = fopen($local_filename . $f, 'w');
              curl_setopt($curl, CURLOPT_URL, $full_remoteURI); #input
              curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($curl, CURLOPT_FILE, $file); #output
              curl_setopt($curl, CURLOPT_RANGE, $start . "-" . ($start + 512000));
              curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $passwrd);
              curl_exec($curl);
              $voltooid = number_format(100 * $start / $fsize, 0, '','');
              print "<p>XML bestanden downloaden $voltooid %.</p>
              <script type=\"text/javascript\">
              <!--
              document.onload = top.location = 'sync.php?f=" . ($f + 1) . "';
              -->
              </script>\n";
              }
              else {
                  $del = unlink($local_filename);
                  if(!$del) print "<p>Oude bestand verwijderen mislukt!</p>\n";
                  $fh = fopen($local_filename, 'w');
                  for($i = 0; $i < $f; $i++) {
                      $data = file_get_contents($local_filename . $i);
                      fwrite($fh, $data);
                      unlink($local_filename . $i);
                  }
                  fclose($fh);
                  print "<p>Downloaden voltooid.</p>\n";
              }
          }
          else print "<p>Login failed</p>\n";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-03
        • 2011-08-04
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        相关资源
        最近更新 更多