Php ftp operations (upload, download, move, create, etc)

Source: Internet
Author: User
Tags ftp connection learn php programming
This article mainly introduces php ftp operations, including FTP upload, FTP download, FTP mobile, and FTP creation, if you are interested, you can refer to this article for details on FTP operations in PHP. 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

 

2. FTP upload class (ftp. php)

 Conn_id = @ ftp_connect ($ FTP_HOST, $ FTP_PORT) or die ("FTP server connection failed"); @ 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_p = 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_p; $ 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

 

The above is all the content of this article. I hope it will help you learn php programming.

For more articles about FTP operations in PHP (such as uploading, downloading, moving, and creating), please follow the PHP Chinese website!

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.