PHP – Download a File from the FTP Server and Save it in a Local File

This following PHP example helps you to download a file from the FTP server and save it to your 
loacal server.

Code Example:

<?php
function ftp_download($ftp_host, $ftp_user, $ftp_pass, $remote_file_path, $local_file_path, $passive = true, $mode = FTP_BINARY) {
    $conn_id = ftp_connect($ftp_host);    
    if ($conn_id) {
		if(@ftp_login($conn_id, $ftp_user, $ftp_pass)){		
			ftp_pasv($conn_id, $passive);	
			if (ftp_get($conn_id, $local_file_path, $remote_file_path, $mode)){
				ftp_close($conn_id);
				return true;
			}
			else {
				ftp_close($conn_id);
				return false;
			}		
		}
		else{
		  echo "Failed to login to the FTP server.";
		}    
    }
	else{
      echo "Failed to connect to the FTP server.";
    }
    
}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

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