Php ftp operations (upload, download, move, create, etc.), phpftp_PHP tutorial

Source: Internet
Author: User
Tags ftp connection ftp file learn php programming
Php ftp operations (upload, download, move, create, etc.), phpftp. Php ftp operations (uploads, downloads, moves, creates, etc.), phpftp This article introduces in detail for php ftp operations, php implements FTP upload, FTP download, FTP mobile, FTP creation, and so on, for you to refer to the php ftp class (upload, download, mobile, 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_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

<?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

Upload (upload, download, move, create, etc.). phpftp This article introduces FTP operations in PHP in detail, php implements FTP upload, FTP download, FTP mobile, FTP creation, etc, for your reference...

Related Article

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.