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