Question

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 ("..");

} 
Was it helpful?

Solution

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.

OTHER TIPS

You can use the following code:

public static function download($local_dir, $remote_dir, $ftp_conn) {

    if ($remote_dir != ".") {
        if (ftp_chdir($ftp_conn, $remote_dir) == false) {
            echo ("Change Dir Failed: $remote_dir \n");
            return;
        }
        if ( !(is_dir($remote_dir))  ){
            mkdir($remote_dir);
            echo "-> Folder created: ".$remote_dir." \n";
        }

        chdir($remote_dir);
    }

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


    foreach ($contents as $file) {

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

        if (@ftp_chdir($ftp_conn, $file)) {
            ftp_chdir($ftp_conn, "..");
            ftpmanager::download($local_dir."/".$file, $file, $ftp_conn);
        }
        else {
            echo "----> path for local file: "."$local_dir/$file"." \n";
            echo "----> path for remote file: ".$file." \n";
            ftp_get($ftp_conn, "$local_dir/$file", "$file", FTP_BINARY);
            echo "-> Downloaded file: ".$local_dir."/".$file." \n";
        }

    }

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

Just insert this static function in an object and call it:

myobject::download( $sBackUpFullCurrentPath, '.', $rConnection );

Hope this help you or others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top