Jump to content

FTP Subdirectory Recursively


jjfletch

Recommended Posts

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

}  

Link to comment
https://forums.phpfreaks.com/topic/178851-ftp-subdirectory-recursively/
Share on other sites

I have never tried this before, so I can't speak about whether it will work or not.  But I did notice, that in the middle of your function you have the directory "./In/" hard-coded in the ftp_nlist function call.  Shouldn't that be $dir?

I have never tried this before, so I can't speak about whether it will work or not.  But I did notice, that in the middle of your function you have the directory "./In/" hard-coded in the ftp_nlist function call.  Shouldn't that be $dir?

 

Yes, it actually is... That was just me trying to see if that would work...  But, it didn't... :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.