使用PHP递归扫描目录和子目录 [英] Recursively scan directory and sub-directories through FTP with PHP

查看:146
本文介绍了使用PHP递归扫描目录和子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying create a list of all files (and their sizes) in a directory including everything within the sub-directories.

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

The files are on a remote server. So my script connects through FTP, then runs a recursive function using ftp_chdir to go through each directory.

如果有另外一种方式是这样做,我可以提出建议。

If there's another way to do this, I'm open to suggestions.

$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 。唯一改变的是我们向服务器添加了更多的文件。如果我在一个子目录下运行该脚本,该脚本将起作用。但如果我运行它。它失败。那么这个失败是因为有太多的文件和子目录?

The script worked fine up to a point, but now it's not working. The PHP returns an error with ftp_chdir. The only thing that changed is that we've added more files to the server. The script works if I run it on a sub-directory. But if I run it on "." it fails. So is this failing because there are too many files and sub-directories?

推荐答案

我没有测试过,但是这里我怎么做了一会儿?

I haven't tested this out, but here's how I did it a while back:

    $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;
    }

这篇关于使用PHP递归扫描目录和子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆