FTP Directory Recursively?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
n00b0101
Forum Newbie
Posts: 1
Joined: Sat Oct 24, 2009 10:41 am

FTP Directory Recursively?

Post by n00b0101 »

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?

Code: Select all

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