/*** Function name: php_ftp_download
* Function to download files from an Ftp server
* Entry parameters
* Filename: the name of the file to be downloaded, including the path
*/
Function php_ftp_download ($ filename ){
$ Phpftp_host = "ftplocalhost"; // server address
$ Phpftp_port = 21; // server port
$ Phpftp_user = "name"; // User name
$ Phpftp_passwd = "passwrd"; // Password
$ Ftp_path = dirname ($ filename). "/"; // obtain the path
$ Select_file = basename ($ filename); // get the file name
$ Ftp = ftp_connect ($ phpftp_host, $ phpftp_port); // connect to the Ftp server
If ($ ftp ){
If (ftp_login ($ ftp, $ phpftp_user, $ phpftp_passwd) {// Login
If (@ ftp_chdir ($ ftp, $ ftp_path) {// enter the specified path
$ Tmpfile = tempnam (getcwd (). "/", "temp"); // create a unique temporary file
If (ftp_get ($ ftp, $ tmpfile, $ select_file, FTP_BINARY) {// download the specified file to a temporary file
Ftp_quit ($ ftp); // close the connection
Header ("Content-Type: application/octet-stream ");
Header ("Content-Disposition: attachment; filename =". $ select_file );
Readfile ($ tmpfile );
Unlink ($ tmpfile); // delete a temporary file
Exit;
}
Unlink ($ tmpfile );
}
}
}
Ftp_quit ($ ftp );
}
?>