PHP ftp file upload and download function

Source: Internet
Author: User
Tags echo date ftp connection ftp file ftp login file transfer protocol
FTP is the abbreviation for file Transfer Protocol (document Transfer Protocol), and Chinese is called "Interfax protocol". Used for two-way transmission of control files on the Internet. At the same time, it is also an application (application). There are different FTP applications based on different operating systems, and all of these applications follow the same protocol to transfer files. In the use of FTP, users often encounter two concepts: "Download" (Download) and "Upload" (Upload). A "Download" file is a copy of a file from a remote host to its own computer; the "upload" file is a copy of the file from your computer to a remote host. In the Internet language, users can upload (download) files to (from) a remote host through a client program.

This article mainly for you in detail the PHP use FTP to achieve file upload and download function, with a certain reference value, interested in small partners can refer to

FTP File Upload

PHP from the function package with FTP operation, a relatively simple implementation of the FTP file upload operation can be done in the following several steps:

1, confirm the FTP server IP address and port information (if you are using the default port can not care);
2, do ftp_connect operation, connect to FTP server (need to note whether the port parameter is set);
3, the Ftp_login operation, the use of FTP user name and password to log in;
4, here began to distinguish, if only need to upload the file, there is no other requirements, that can be done here to upload the ftp_put operation of the file, if the upload file has to be stored in accordance with the needs of the directory, then continue to proceed downward;
5. Use Ftp_nlist to get the directory and file name under the given FTP directory, check if the required directory exists, and if it does not exist, Ftp_mkdir to create the directory;
6, switch to the target directory ftp_chdir;
7, the ftp_put operation upload files;
8, make Ftp_close close FTP connection.

In order to upload files in FTP according to the date format directory, do a simple code implementation:

<?php$host = ' 10.0.0.42 '; $user = ' uftp '; $pwd = ' uftp ';//FTP connection, depending on whether the port is set, the parameters passed will be different if (empty ($port)) {$f _conn = Ftp_ Connect ($host);} else{$f _conn = Ftp_connect ($host, $port);}    if (! $f _conn) {echo "Connect fail\n"; Exit (1);}    echo "Connect success\n";//FTP login with the given FTP login username and password for login$f_login = Ftp_login ($f _conn, $user, $pwd); if (! $f _login) {    echo "Login fail\n"; Exit (1);}    echo "Login success\n";//Get current FTP directory $in_dir = ftp_pwd ($f _conn), if (! $in _dir) {echo "Get dir info fail\n"; Exit (1);} echo "$in _dir\n";//Gets the directory and file $exist_dir = ftp_nlist ($f _conn, ftp_pwd ($f _conn)) contained in the current FTP directory,;p rint_r ($exist _dir);// The request is to create a folder in the FTP directory as the file upload directory echo date ("YMD") by date. \ n "; $dir _name = Date (" YMD ");//Check if the FTP directory already exists with the current date of the folder, if it does not exist then create if (!in_array (" $in _dir/$dir _name ", $exist _dir)) {if (!        Ftp_mkdir ($f _conn, $dir _name)) {echo "mkdir fail\n";    Exit (1);    }else{echo "mkdir $dir _name success\n";    }}//Switch Directory if (!ftp_chdir ($f _conn, $dir _name)) {echo "ChDir fail\n"; Exit (1);} Else{echo "ChDir $dir _name success\n";} File Upload $result = ftp_put ($f _conn, ' Bbb.mp3 ', '/root/liang/ftp/bbb.mp3 ', ftp_binary), if (! $result) {echo "Upload file F    Ail\n "; Exit (1);}    else{echo "Upload file success\n"; Exit (0);}

Print:

root@webdevelop232:~/liang/ftp# php ftp.php Connect successlogin success/home/uftparray (  [0] =/home/uftp/ Kalimba.mp3  [1] =/home/uftp/test.txt) 20170721mkdir 20170721 successchdir 20170721 successupload file success

You can see the print operation is successful, and then go to the FTP server directory, you can see the uploaded files.

FTP File download

Compared to the file, the use of PHP for FTP file download is very rare, but since this feature, it is always possible to use, so also do a simple example.

Take the Bbb.mp3 file uploaded above as the download target, download it to the current directory, and name it 1.mp3:

<?php$host = ' 10.0.0.42 '; $uname = ' uftp '; $upwd = ' uftp ';//FTP connection if (empty ($ Port) {$f _conn = Ftp_connect ($host);} else{$f _conn = Ftp_connect ($host, $port);}  if (! $f _conn) {echo "FTP connect fail\n"; Exit (1);}  FTP Login if (!ftp_login ($f _conn, $uname, $upwd)) {echo "FTP login fail\n"; Exit (1);} FTP Download if (!ftp_get ($f _conn, './1.mp3 ', ftp_pwd ($f _conn). ' /'. Date (' Ymd '). '  /bbb.mp3 ', ftp_binary) {echo "FTP download fail\n"; Exit (1);}  else{echo "ftp download success\n"; Exit (0);} 

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.