ftp_get a directory

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
Leb_CRX
Forum Newbie
Posts: 9
Joined: Thu May 18, 2006 8:27 am

ftp_get a directory

Post by Leb_CRX »

I've been busting my brain trying to get something to work, but it's been a long day and I'm at the same place as when I started

does anyone have a function that works? basically, I want to call it with the conn_id, the remote path, and the local path, and have it fetch me all the files

any help appreciated
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: ftp_get a directory

Post by Eric! »

How are you trying it now?

Code: Select all

$connect = ftp_connect("ftp.hostname.com");
$result = ftp_login($connect, "username", "password");
$a = ftp_nlist($connect, ".");// put your path where the . is
 foreach($a as $value){
   echo $value, "<BR>";
   }
 
If this returns the list you want put the ftp_get($value,$savedlocalvalue) instead of the echo.
Leb_CRX
Forum Newbie
Posts: 9
Joined: Thu May 18, 2006 8:27 am

Re: ftp_get a directory

Post by Leb_CRX »

Eric! wrote:How are you trying it now?

Code: Select all

$connect = ftp_connect("ftp.hostname.com");
$result = ftp_login($connect, "username", "password");
$a = ftp_nlist($connect, ".");// put your path where the . is
 foreach($a as $value){
   echo $value, "<BR>";
   }
 
If this returns the list you want put the ftp_get($value,$savedlocalvalue) instead of the echo.

I've tried a few functions, the latest one is this:

looks pretty simular to yours...the thing that gets me is I need it to crawl the directories and sub directories and keep going to get everything

Code: Select all

 
    function backupLiveSite(){
        $ftp_server     = $this->ftp_live_server;
        $ftp_username   = $this->ftp_live_username;
        $ftp_password   = $this->ftp_live_password; 
        $backup_dir_on_cms = G_FTP_CMS_BACKUP_DIR;
        $external_live_dir = "/";
 
        if ($conn_id = ftp_connect($ftp_server,21)) {
            // Yes - Login successful?
            if (ftp_login($conn_id, $ftp_username, $ftp_password)) {
                $this->transferFiles($conn_id, $external_live_dir,  $backup_dir_on_cms);
            } else {
                return false;
            }
        ftp_close($conn_id);
        }
    }
    
    function transferFiles($conn_id, $dirRemote, $dirLocal){
 
      // get contents of the current directory
      $contents = ftp_nlist($conn_id, $dirRemote);
      
      foreach($contents as $file){
         $file2 = "$dirRemote/$file";
         $file3 = "$dirLocal/$file";
         echo "file2:".$file2;
         echo "<br>file3:".$file3;
         
         if ( (ftp_size($conn_id, $file2) == -1) && !( ($file  == ".") || ($file == "..") ) ){ //Directory
            echo "<b>$file</b><br>";
            mkdir($file3);   //Make Folder
            $this->transferFiles($conn_id, $file2,$file3);
         }
         elseif (ftp_size($conn_id, $file2) > 0) {
            echo "<i>$file</i>";
            
            //If File Exists Skip.
            if (file_exists($file3)){
               if (ftp_size($conn_id,$file2) == filesize($file3))
                  echo "- skipped <br>";
               else {
                  ftp_get($conn_id,$file3,$file2,FTP_BINARY,filesize($file3)/9);
                  echo "- completed <br>";
               }
            }
            else {
               ftp_get($conn_id,$file3,$file2,FTP_BINARY);
               echo "<br>";
            }
            
         }
   }
Post Reply