views:

498

answers:

1

The code below works insomuch that I can successfully download the directory recursively. But, I want to download the directories within this directory. So, when it connects it's in . Within the . directory is a subdirectory "In". I want to recursively retrieve the contents within the In directory. The directory names themselves will change, so I can't specify what that's going to be in the script itself... Anyone know how to do this?

ftp_sync ("./In/");    
ftp_close($conn_id); 

function ftp_sync ($dir) {

    global $conn_id;

    if ($dir != ".") {
        if (ftp_chdir($conn_id, $dir) == false) {
            echo ("Change Dir Failed: $dir<BR>\r\n");
            return;
        }
        if (!(is_dir($dir)))
            mkdir($dir);
        chdir ($dir);
    }

    $contents = ftp_nlist($conn_id, "./In/");
    foreach ($contents as $file) {

        if ($file == '.' || $file == '..')
            continue;

        if (@ftp_chdir($conn_id, $file)) {
            ftp_chdir ($conn_id, "..");
            ftp_sync ($file);
        }
        else
            ftp_get($conn_id, $file, $file, FTP_BINARY);
    }

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

}
+2  A: 

You could use the RecursiveDirectoryIterator class:

$ite=new RecursiveDirectoryIterator("/path/");

$bytestotal=0;
$nbfiles=0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
    $filesize=$cur->getSize();
    $bytestotal+=$filesize;
    $nbfiles++;
    echo "$filename => $filesize\n";
}

Example stolen from http://www.php.net/manual/en/class.recursivedirectoryiterator.php, and accommodate it to your liking.

meder