【问题标题】:Cant download some files with PHP FTP无法使用 PHP FTP 下载某些文件
【发布时间】:2013-01-17 15:55:33
【问题描述】:

我编写了一个 php 脚本来通过 FTP 下载文件和文件夹,这样我就可以在某些条件下进行备份。但是,它不是下载所有文件。它正在从我们的服务器下载一些图像。我无法理解其他图像有什么问题。它们都是各国国旗的小图像。它们大小相同。如果我从更多根级别下载,它会下载其他文件。但是,它恰好从 co.png 文件停止。

这里是link (http://robi123.com/flags.zip),用于下载我尝试使用 PHP FTP 下载的图像文件。

我知道这个脚本可能需要其他改进,或者在下载其他大文件时也会卡在其他步骤中。我现在需要帮助来解决这个问题。我的脚本没有下载其他图像是什么?这个问题附有php的警告。我还有一个想法可以尝试执行 RAW FTP 命令。忽略 RAW FTP 命令的代码。但是 ftp 下载的所有其他方法(ftp_get、ftp_nb_get 和)都卡在同样的事情和同样的警告中。但是您可以提供任何建议。

这是我的代码:

<?php
set_time_limit(0);
ini_set('xdebug.max_nesting_level', "100000000000000000000000");

$ftp_server = "";
$ftp_user    = "";
$ftp_pass = "";

$ftp_conn   = ftp_connect($ftp_server);
$ftp_login   = ftp_login($ftp_conn, $ftp_user, $ftp_pass);

ftp_set_option($ftp_conn, FTP_TIMEOUT_SEC, 10000000);

ftp_set_option($ftp_conn, FTP_AUTOSEEK, true);

ftp_pasv($ftp_conn, true);

if ((!$ftp_conn) || (!$ftp_login)) {
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user<br />";
} else {
    echo "Connected to $ftp_server, for user $ftp_user<br />";
}


echo getcwd()."<br />" ;

function download($ftp_conn,$local_folder,$remote_folder){

     if($local_folder !=""){
     chdir($local_folder);
     }
     echo getcwd()."<br />" ;

     ftp_chdir($ftp_conn,$remote_folder);

     $files_folders = ftp_nlist($ftp_conn, ".");

     foreach($files_folders as $temp){

      if ($temp == '.' OR $temp == '..'){ 
      continue;
      }
      if(ftp_is_dir($ftp_conn,$temp)){
      //echo "$temp<br>";

      @mkdir($temp);

      echo "Folder: ".$remote_folder."/".$temp."<br />";

      download($ftp_conn,$temp,$remote_folder."/".$temp);

      }else{
      ftp_get($ftp_conn, $temp, $temp, FTP_BINARY);
      //ftp_nb_get($ftp_conn, $temp, $temp, FTP_BINARY); 

      /*

        $ret = ftp_nb_get($ftp_conn, $temp, $temp, FTP_BINARY);
        while ($ret == FTP_MOREDATA) {

        $ret = ftp_nb_continue($ftp_conn);
        }
        if ($ret != FTP_FINISHED) {
        echo "There was an error downloading the file";
        exit(1);
        }

      $command = "RETR $remote_folder/$temp";
      $result = ftp_raw($ftp_conn, trim($command));  
      print_r($result);
       */
      echo "File: $temp<br />";

      }

     }

     chdir("..");
     ftp_chdir($ftp_conn, "..");

     return;

}

function ftp_is_dir($ftp_conn,$dir) {
  if (@ftp_chdir($ftp_conn, $dir)) {
    ftp_chdir($ftp_conn, '..');
    return true;
  } else {
    return false;
  }
}

download($ftp_conn,"data","/public_html/c4smod/traffic/Img/Flags/");

?>

【问题讨论】:

  • 为什么要隐藏所有错误?当您查找错误时,@ 是个坏主意 :)
  • 警告信息是什么?你的屏幕截图只是说“警告”,这完全没用。你的脚本中的第 54 行是什么?
  • ftp_get($ftp_conn, $temp, $temp, FTP_BINARY); - 第 54 行。 ftp_get 不返回除 true 和 false 之外的任何其他内容。这就是为什么很难找到问题所在。
  • mkdir 的警告在这种情况下无关紧要。但它们稍后会通过真/假检查正确更新。
  • @ftp_chdir($ftp_conn, $dir) 在这里非常棘手。我们必须使用@ 来忽略错误。否则,该函数将无法返回 true 或 false。

标签: php windows apache ftp


【解决方案1】:

我发现了问题。在我下载了几张图片后,我的脚本只需要重新连接到服务器并下载其余的图片。所以,我只是添加了额外的条件来检查 ftp 功能是否失败并且它们被重新连接并重试以继续下载文件和图像。我没有强烈尝试进一步查找并知道 ftp 会话或超时存在多长时间,或者这些问题是否与我的脚本有关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2011-08-04
    • 2012-03-04
    相关资源
    最近更新 更多