PHP - 从FTP中的所有目录中获取所有文件

时间:2015-01-16 15:14:10

标签: php ftp

我有这个疑问,我可以从特定目录下载所有文件,但是如何从特定文件夹中的所有目录下载所有文件? 我从目录下载的代码是这样的:

$ftp_server = "server";
$ftp_user = "user";
$ftp_pass = "pass";

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

function get_file($strDir) {
    global $conn_id;
    $contents = ftp_nlist($conn_id, $strDir);   
    for($i=0; $i<count($contents); $i++) {
        if (strstr($contents[$i], "OUT") !== FALSE) {
            if (ftp_get($conn_id, substr($contents[$i],1), $contents[$i], FTP_ASCII)) {
                echo "Successfully written to ".substr($contents[$i],1)."<br>";
            } else {
                echo "There was a problem ".substr($contents[$i],1)."<br>";
            }

        }
        else {
            mkdir(substr($contents[$i],1),0777,true);                   
            $a = get_file($contents[$i]);
        }
    }
    return 1;
}

if (ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    $contents = ftp_nlist($conn_id, "OUT");
    for($i=0; $i<count($contents); $i++) {
            echo "Directory = " . $contents[$i] . "<br>";   
            ftp_get($conn_id, $contents[$i], $contents[$i], FTP_BINARY);
    }
}

那么,如何从所有目录下载所有文件?

1 个答案:

答案 0 :(得分:0)

您可以使用 RecursiveDirectoryIterator 获取特定目录的子目录中的所有文件

  

http://php.net/manual/en/class.recursivedirectoryiterator.php