Php ftp operations (upload, download, move, create, etc.), phpftp
This article introduces FTP operations in PHP in detail. php implements FTP upload, FTP download, FTP mobile, and FTP creation for your reference. The specific content is as follows:
1. Use PHP to operate FTP-usage
<? Php // connect to the FTP server $ conn = ftp_connect (ftp.server.com); // use username and password to log on to ftp_login ($ conn, "john", "doe "); // obtain the remote system type ftp_1_ype ($ conn); // list the file $ filelist = ftp_nlist ($ conn, ". "); // download the file ftp_get ($ conn, “data.zip", “data.zip ", FTP_BINARY); // close the connection ftp_quit ($ conn); // you can create an FTP connection, PHP provides the ftp_connect () function, which uses the host name and port as the parameter. In the preceding example, the host name is "ftp.server.com". If the port is not specified, PHP uses "21" as the default port to establish a connection. // After the connection is successful, ftp_connect () returns a handle. This handle will be used by subsequent FTP functions. $ Conn = ftp_connect (ftp.server.com); // once a connection is established, use ftp_login () to send a user name and password. As you can see, this function ftp_login () uses handle from the ftp_connect () function to confirm that the user name and password can be submitted to the correct server. Ftp_login ($ conn, "john", "doe"); // close connection ftp_quit ($ conn); // you have logged on to the FTP server. PHP provides some functions, they can obtain information about systems, files, and directories. Ftp_pwd () // obtain the current directory $ here = ftp_pwd ($ conn); // obtain the server-side system information ftp_0000ype () $ server_ OS = ftp_0000ype ($ conn ); // passive mode (PASV) switch, enable or disable PASV (1 indicates on) ftp_pasv ($ conn, 1); // use the ftp_chdir () function in the directory, it accepts a directory name as a parameter. Ftp_chdir ($ conn, "public_html"); // return to the parent directory of the directory. Use ftp_cdup () to implement ftp_cdup ($ conn); // create or move a directory, this requires the ftp_mkdir () and ftp_rmdir () functions. Note: If ftp_mkdir () is successfully created, the new directory name will be returned. Ftp_mkdir ($ conn, "test"); ftp_rmdir ($ conn, "test"); // The ftp_put () function is competent to upload files, it requires you to specify a local file name, the uploaded file name, and the transfer type. For example, if you want to upload the “abc.txt file, upload the file named "“xyz.txt", the command should be: ftp_put ($ conn, “xyz.txt ", “abc.txt", FTP_ASCII); // download the file: the function provided by PHP is ftp_get (). It also requires a file name on the server, the downloaded file name, and the transmission type as the parameter. For example, if the server file is his.zip, download it to the terminal and name it hers.zip. The command is as follows: ftp_get ($ conn, “hers.zip ", “his.zip", FTP_BINARY); // PHP provides two methods: a simple list of file names and directories, the other is to list the file size, permissions, creation time, and other information in detail. // The first method uses the ftp_nlist () function, and the second method uses ftp_rawlist (). both functions require a directory name as the parameter. Both return the directory column as an array, and each element of the array is equivalent to a row in the list. $ Filelist = ftp_nlist ($ conn, "."); // function ftp_size (), which returns the size of the specified file, in BITES. It should be noted that if it returns "-1", it means that this is a directory $ filelist = ftp_size ($ conn, “data.zip ");?>
2. FTP upload class (ftp. php)
<? Php /************************************** * ****** MODULE: FTP ************************************** * ***/class ftp {public $ off; // return the operation status (successful/failed) public $ conn_id; // FTP connection/*** method: FTP connection * @ FTP_HOST -- FTP host * @ FTP_PORT -- Port * @ FTP_USER -- user name * @ FTP_PASS -- password */function _ construct ($ FTP_HOST, $ FTP_PORT, $ FTP_USER, $ FTP_PASS) {$ this-> conn_id = @ ftp_connect ($ FTP_HOST, $ FTP_PORT) or die ("FTP server connection Failed to connect "); @ ftp_login ($ this-> conn_id, $ FTP_USER, $ FTP_PASS) or die (" FTP Server login failed "); @ ftp_pasv ($ this-> conn_id, 1); // enable passive simulation}/*** method: upload File * @ path -- local path * @ newpath -- upload path * @ type -- if the target directory does not exist, create */function up_file ($ path, $ newpath, $ type = true) {if ($ type) $ this-> dir_mkdirs ($ newpath); $ this-> off = @ ftp_put ($ this-> conn_id, $ newpath, $ path, FTP_BINARY ); if (! $ This-> off) echo "File Upload Failed. Check whether the permission and path are correct! ";}/*** Method: move file * @ path -- original path * @ newpath -- New path * @ type -- if the target directory does not exist, create */function move_file ($ path, $ newpath, $ type = true) {if ($ type) $ this-> dir_mkdirs ($ newpath); $ this-> off = @ ftp_rename ($ this-> conn_id, $ path, $ newpath ); if (! $ This-> off) echo "failed to move the file. Check whether the permission and original path are correct! ";}/*** Method: copy file * Description: As FTP does not have a copy command, the work und of this method is: download the package and upload it to the new path * @ path -- original path * @ newpath -- New path * @ type -- if the target directory does not exist, create */function copy_file ($ path, $ newpath, $ type = true) {$ downpath = "c:/tmp. dat "; $ this-> off = @ ftp_get ($ this-> conn_id, $ downpath, $ path, FTP_BINARY); // download if (! $ This-> off) echo "failed to copy the file. Check whether the permission and original path are correct! "; $ This-> up_file ($ downpath, $ newpath, $ type);}/*** method: delete a file * @ path -- path */function del_file ($ path) {$ this-> off = @ ftp_delete ($ this-> conn_id, $ path); if (! $ This-> off) echo "An error occurred while deleting the file. Check whether the permission and path are correct! ";}/*** Method: generate the directory * @ path -- path */function dir_mkdirs ($ path) {$ path_arr = explode ('/', $ path ); // get the directory array $ file_name = array_pop ($ path_arr); // pop up the file name $ path_div = count ($ path_arr); // get the layers foreach ($ path_arr as $ val) // create a directory {if (@ ftp_chdir ($ this-> conn_id, $ val) = FALSE) {$ tmp = @ ftp_mkdir ($ this-> conn_id, $ val ); if ($ tmp = FALSE) {echo "Directory creation failed. Check whether the permission and path are correct! "; Exit ;}@ ftp_chdir ($ this-> conn_id, $ val) ;}}for ($ I = 1; $ I <=$ path_div; $ I ++) // roll back to the root {@ ftp_cdup ($ this-> conn_id) ;}}/*** method: close the FTP connection */function close () {@ ftp_close ($ this-> conn_id) ;}// class class_ftp end
/************************************ Test * ********************************** $ ftp = new ftp ('2017. 13.67.42 ', 21, 'hlj', '000000'); // open the FTP connection $ ftp-> up_file('aa.wav', 'test/123456/bb.wav '); // upload a file // $ ftp-> move_file ('aaa/aaa. php', 'aaa. php '); // move the file // $ ftp-> copy_file ('aaa. php', 'aaa/aaa. php '); // copy the file // $ ftp-> del_file ('aaa. php '); // delete the file $ ftp-> close (); // close the FTP connection //********************************* **************************************** *****/
3. Create a directory using FTP functions in PHP
<?php // create directory through FTP connection function FtpMkdir($path, $newDir) { $server='ftp.yourserver.com'; // ftp server $connection = ftp_connect($server); // connection // login to ftp server $user = "me"; $pass = "password"; $result = ftp_login($connection, $user, $pass); // check if connection was made if ((!$connection) || (!$result)) { return false; exit(); } else { ftp_chdir($connection, $path); // go to destination dir if(ftp_mkdir($connection,$newDir)) { // create directory return $newDir; } else { return false; } ftp_close($conn_id); // close connection } } ?>
The above is all the content of this article. I hope it will help you learn php programming.
Articles you may be interested in:
- Code for online Ftp user management using PHP
- Bplaced Germany can bind M 2 GB support FTP free PHP Space
- Php program for downloading the file tree from the ftp server to the Local Computer
- Connect to ftp in php to upload, download, and delete files.
- Php ftp file upload function (Basic Edition)
- No need to re-compile php to add ftp extension Solution
- In-depth explanation of the php ftp class
- Win2008 r2 server environment configuration (FTP/ASP. Net/PHP)
- Php ftp operation code (upload, copy, move, delete file/create directory)
- Example of ftp File Upload using PHP