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
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?

Link to comment
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?

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.