【问题标题】:Recursively scan directory and sub-directories through FTP with PHP使用PHP通过FTP递归扫描目录和子目录
【发布时间】:2011-05-05 20:54:36
【问题描述】:

我正在尝试创建一个目录中所有文件(及其大小)的列表,包括子目录中的所有内容。

文件位于远程服务器上。所以我的脚本通过 FTP 连接,然后使用ftp_chdir 运行递归函数来遍历每个目录。

如果有其他方法可以做到这一点,我愿意接受建议。

$flist = array();

function recursive_list_dir($conn_id, $dir, $parent = "false") {
 global $flist;
 ftp_chdir($conn_id, $dir) or die("Fudgeballs: ".$parent."/".$dir);
 $list = array();
 $list = ftp_rawlist($conn_id, ".");

 if($parent != "false") { $dir = $parent."/".$dir; }

 for($x = 0; $x < count($list); $x++) {
  $list_details = preg_split("/[\s]+/", $list[$x]);
  $file = $list_details[3];
  $size = $list_details[2];

  if(!strstr($file, ".")) { // if there's no dot (.), then we assume it's a directory (is there a command similar to "is_dir" for FTP? that would be more fail proof?)
   recursive_list_dir($conn_id, $file, $dir);
  }
  else { $flist[] = $dir."@".$file."@".$size; }
 }
 ftp_chdir($conn_id, "..");
}

recursive_list_dir($conn_id, ".");

该脚本在一定程度上运行良好,但现在无法运行。 PHP 返回带有ftp_chdir 的错误。唯一改变的是我们向服务器添加了更多文件。如果我在子目录上运行该脚本,它就可以工作。但如果我在“。”上运行它。它失败。那么这是因为文件和子目录太多而失败了吗?

【问题讨论】:

  • 我假设你不能在远程服务器上运行任何东西? (也没有php脚本左右)

标签: php recursion ftp directory


【解决方案1】:

我还没有对此进行测试,但这是我不久前的做法:

    $hostname = 'write.your.server.here';
    $username = 'username';
    $password = 'password';
    $startdir = 'starting/directory'; // absolute path
    $suffix   = "gif,png,jpeg,pdf,php"; // suffixes to list
    $files = array();
    $conn_id = ftp_connect($hostname);
    $login = ftp_login($conn_id, $username, $password);
    if (!$conn_id) {
        echo 'Wrong server!';
        exit;
    } else if (!$login) {
        echo 'Wrong username/password!';
        exit;
    } else {

        $files = raw_list("$startdir");
    }

    ftp_close($conn_id);

    function raw_list($folder) {
        global $conn_id;
        global $suffix;
        global $files;
        $suffixes = explode(",", $suffix);
        $list     = ftp_rawlist($conn_id, $folder);
        $anzlist  = count($list);
        $i = 0;
        while ($i < $anzlist) {
            $split    = preg_split("/[\s]+/", $list[$i], 9, PREG_SPLIT_NO_EMPTY);
            $itemname = $split[8];
            $endung   = strtolower(substr(strrchr($itemname ,"."),1));
            $path     = "$folder/$itemname";
            if (substr($list[$i],0,1) === "d" AND substr($itemname,0,1) != ".") {
                raw_list($path);
            } else if(substr($itemname,0,2) != "._" AND in_array($endung,$suffixes)) {
                array_push($files, $path);
            }
            $i++;
        }
        return $files;
    }

【讨论】:

    【解决方案2】:

    在您提供更多输入之前它正在工作的事实在我看来似乎表明这可能是一个问题。尝试将set_time_limit(300); 放在顶部,让它在超时前运行 5 分钟,看看是否能解决问题。

    【讨论】:

    • PHP 手册说默认时间限制是 30 秒。而我服务器上 PHP 的 max_execution_time 是 120。当我的脚本过去运行时,它需要一段时间才能完成 - 超过 120 秒。这怎么可能? PHP 是否对单个命令或整个脚本计时?我尝试将其设置为 300 秒,但没有任何改变。
    【解决方案3】:

    真正的不使用全局变量的递归解决方案:

    function ftp_list_files_recursive($ftp_stream, $path)
    {
        $lines = ftp_rawlist($ftp_stream, $path);
    
        $result = array();
    
        foreach ($lines as $line)
        {
            $tokens = explode(" ", $line);
            $name = $tokens[count($tokens) - 1];
            $type = $tokens[0][0];
    
            $filepath = $path . "/" . $name;
            if ($type == 'd')
            {
                $result = array_merge($result, ftp_list_files_recursive($ftp_stream, $filepath));
            }
            else
            {
                $result[] = $filepath;
            }
        }
        return $result;
    }
    

    适用于使用常见 *nix 样式列表的 FTP 服务器,例如:

    -r--r--r-- 1 ftp ftp             13 Nov 09  2015 file.txt
    dr-xr-xr-x 1 ftp ftp              0 Nov 10  2015 folder
    

    不适用于名称中有空格的文件。

    【讨论】:

      【解决方案4】:

      在 PHP 中使用全局变量并不是一个好习惯。看到这个:

      function ftp_get_files_list( $conn_id, $baseDir='.' ) {
          $files = array();
          $dirs = array($baseDir);
          while( $dir = array_shift($dirs) ) {
              $list = ftp_rawlist( $conn_id, $dir);
              while( $line = array_shift($list) ) {
                  $col = preg_split( "@\s+@", $line );
                  if (count($col) <= 2) continue;
                  $fname = implode(' ',array_slice($col,8)); // support filenames with spaces
                  $isDir =($col[0][0]=='d');
                  if ($isDir)
                      array_push($dirs, $dir.'/'.$fname );
                  else
                      array_push($files, $dir.'/'.$fname );
              }
          }
          return $files;
      }
      

      【讨论】:

        猜你喜欢
        • 2010-12-09
        • 2012-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 1970-01-01
        • 2013-04-30
        • 2014-10-01
        相关资源
        最近更新 更多