Copy files from other ftp or server to current ftp or server

Steps to copy all files of a particular folder from other ftp to current ftp –

First of all make connection with Remote(other) FTP (from where we are copying the files) like –
$ftp_host = ‘host’;
$ftp_username = ‘ftp_username’;
$ftp_password = ‘ftp_password’;
$ftp_connection = ftp_connect($ftp_host);

Then, login to Remote or other ftp using –
$ftp_login = ftp_login($ftp_connection,$ftp_username,$ftp_password);

After that get all files of that directory of remote ftp –
$remote_dir = ‘/dir/’;
$remote_files = ftp_nlist($ftp_connection,$remote_dir); //Array of all files from dir directory on remote server

Then, check that file exists in that directory on not –
if(count($remote_files)>0){ //There are files in remote server dir
foreach($remote_files as $remote_file){ //run loop through each file
$file_name = end(“/”,explode($remote_file)); //get file name from remote
$local_file = ‘local_dir/’.$file_name;
$server_file = $remote_file; //file from remote server
if(ftp_get($ftp_connection,$local_file,$server_file, FTP_ASCII)){
echo “File successfully copied”;
}else{
echo “error”;
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *