Zend - The PHP Company




Files and Directories

Add Code


Function ftp_dget() for downloading direcotry of files via FTP  

Type: application
Added by: HitByASquirrel
Entered: 15/06/2003
Last modified: 09/12/2002
Rating: - (fewer than 3 votes)
Views: 5134
This function wil download all files and subdirectories recursively if need be from a specified directory on a aremote server. It works just like other PHP ftp functions.


<?
/****************************************************************************
This code v1.0 is Copyright 2003 by Jon Bohren and Waveforme Backend Technologies
jon@waveforme.com

This function, ftp_dget(), willl download a directory and all it's contents.
You can choose to download sub directories or not by passing TRUE or FALSE
into $subd.

Feel free to use or edit this code, but please keep the copyright. Thanks!

Enjoy.
*****************************************************************************/


function ftp_dget($conn_id$dir_name_local$dir_name_server$subd$mode) {
    
//get file list for directory to be downloaded    
    
$ls_dir ftp_rawlist($conn_id$dir_name_server);
    
    
//create local directory
    
if(!file_exists($dir_name_local))
        
$new_dir mkdir($dir_name_local0777);
    
$curr_dir chdir($dir_name_local);
    
    
//get rid of sub directories in file array if $subd is FALSE
    
if(!$subd) {
        
$s 0;
        for(
$i 0$i sizeof($ls_dir); $i++) {
            if(
$ls_dir[$i][0] != 'd') {
                
$ls_wsd[$s] = $ls_dir[$i];
                
$s++;
            }
        }
        
$ls_dir $ls_wsd;
    }
    
    
//download all files in the current directory, recursively call ftrp_dget() to downlaod sub-directories
    
for($i 0$i sizeof($ls_dir); $i++) {
        
$curr_f_name substr($ls_dir[$i], 55); //get actual file name
        
        
if($subd && ($ls_dir[$i][0] == 'd')) {
            
$dir_dl ftp_dget($conn_id$curr_f_name$dir_name_server."/".$curr_f_name$subd$mode)or die("Couldn't download '".$curr_f_name."' via FTP"); //recursively call ftp_dget
        
} else {
            
$file_dl ftp_get($conn_id$curr_f_name$dir_name_server."/".$curr_f_name$mode)or die("Couldn't download '".$curr_f_name."' via FTP"); //call ftp_get to downlaod the files in the current dir
        
}
        
        if(!(
$file_dl || $dir_dl))
            return 
FALSE;
    }
    
    
//return to previous directory
    
chdir("..");
    
    return 
TRUE;
}

$conn_id ftp_connect($ftp_host) or die("Couldn't connect to '".$ftp_host."'");
ftp_login($conn_id$ftp_user$ftp_pass) or die("Couldn't connect to the FTP server as '".$ftp_user."'");
        
ftp_dget($conn_id$local_dir$remote_dir$downlaod_sub_directories$mode);        

?>


Usage Example




Rate This Script





Search



This Category All Categories