Download files from the ftp server

Source: Internet
Author: User

 /**

* 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 );

}

?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.