This article mainly introduces the PHP operation FTP class, the implementation of FTP upload, FTP download, FTP Mobile, FTP creation, etc., interested in small partners can refer to
1. Using PHP to manipulate ftp-usage
<?php//Join FTP Server $conn = Ftp_connect (ftp.server.com); Use username and password to log in to Ftp_login ($conn, "John", "Doe"); Gets the remote system type Ftp_systype ($conn); List Files $filelist = Ftp_nlist ($conn, "."); Download file Ftp_get ($conn, "Data.zip", "Data.zip", ftp_binary); Close the Join Ftp_quit ($conn); Initially knot an FTP connection, PHP provides the Ftp_connect () function, which uses the host name and port as the parameter. In the above example, the host name is "Ftp.server.com", and if the port is not specified, PHP will use "21" as the default port to establish the join. After the join succeeds, Ftp_connect () returns a handle handle, and the handle will be used by the FTP function that is used later. $conn = Ftp_connect (ftp.server.com); Once the join is established, use Ftp_login () to send a user name and user password. As you can see, this function Ftp_login () uses the handle from the Ftp_connect () function to determine that the user name and password can be submitted to the correct server. Ftp_login ($conn, "John", "Doe"); Close connection ftp_quit ($conn); Log in to the FTP server, PHP provides a number of functions, they can get some information about the system and files and directories. FTP_PWD ()//Get the current directory $here = Ftp_pwd ($conn); Get server-side System Information Ftp_systype () $server _os = Ftp_systype ($conn); Passive mode (PASV) switch, turn on or off PASV (1 for Open) FTP_PASV ($conn, 1); Enter the directory with the Ftp_chdir () function, which accepts a directory name as a parameter. Ftp_chdir ($conn, "public_html"); Go back to the directory parent directory with Ftp_cdup () to implement FTP_CDUP ($conn); Create or move a directory that uses the Ftp_mkdir () and Ftp_rmdir () functions; note: if Ftp_mkdir () is successful, the newly created directory name will be returned. Ftp_mkdir ($conn, "test"); Ftp_rmdir ($conn, "test"); Upload file, Ftp_put () function is very good, it requires you to specify a local file name, upload the file name and the type of transmission. For example: If you want to upload "abc.txt" This file, after uploading the name "Xyz.txt", the command should be this: Ftp_put ($conn, "Xyz.txt", "Abc.txt", ftp_ascii); Download file: PHP provides a function is Ftp_get (), it also needs a server file name, download the file name, as well as the transport type as parameters, for example: server-side file for His.zip, you want to download to the local machine, and named Hers.zip, the command is as follows: FTP_ Get ($conn, "Hers.zip", "His.zip", ftp_binary); PHP offers two methods: one is to simply list file names and directories, and the other is to specify the size of files, permissions, creation time and other information. The first one uses the Ftp_nlist () function, and the second uses ftp_rawlist (). Two functions require a directory name as a parameter, return the catalog column as an array, each element of the array is equivalent to a row of the list. $filelist = Ftp_nlist ($conn, "."); function Ftp_size (), which returns the size of the file you specify, using bites as the unit. To point out, 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 *******************************************/ Class FTP {public $off; Return operation status (Success/failure) public $conn _id; FTP Connection/** * Method: FTP Connection * @FTP_HOST--FTP host * @FTP_PORT--Port * @FTP_USER--User name * @FTP_PASS--Password */F Unction __construct ($FTP _host, $FTP _port, $FTP _user, $FTP _pass) {$this->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); Open Passive Impersonation}/** * Method: Upload file * @path--Local Path * @newpath--Upload path * @type--If the target directory does not exist the new */function Up_fil E ($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, please check the permissions and path is correct!" "; }/** * Method: Move File * @path--The original path * @newpath--New path * @type--If the target directory does not exist the new */ 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 "File move failed, please check the permissions and the original path is correct!" "; }/** * Method: Copy File * Description: Due to FTP no copy command, this workaround is: Download and then upload to the new path * @path-the original path * @newpath-New path * @type-if the target directory does not exist NEW */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 "File copy failed, Please check the permissions and the original path is correct! "; $this->up_file ($downpath, $newpath, $type); }/** * Method: Delete file * @path--Path */function Del_file ($path) {$this->off = @ftp_delete ($this->conn_i D, $path); if (! $this->off) echo "File deletion failed, please check the permissions and path is correct!" "; }/** * Method: Generate directory * @path-Path */function Dir_mkdirs ($path) {$path _arr = explode ('/', $path); Fetch directory array $file _name = Array_pop ($path _arr); Pop-up file name $path _p = CounT ($path _arr); Number of layers foreach ($path _arr as $val)//Create directory {if (@ftp_chdir ($this->conn_id, $val) = = FALSE) {$tmp = @ftp_mkdir ($this->conn_id, $val); if ($tmp = = FALSE) {echo ' Directory creation failed, check the permissions and path are correct! "; Exit } @ftp_chdir ($this->conn_id, $val); }} for ($i =1; $i <= $path _p; $i + +)//fallback to root {@ftp_cdup ($this->conn_id); }}/** * method: Close FTP connection */function Close () {@ftp_close ($this->conn_id); }}//Class Class_ftp End
/************************************** Test *********************************** $ftp = new ftp (' 222.13.67.42 ', 21, ' Hlj ', ' 123456 '); Open FTP connection $ftp->up_file (' aa.wav ', ' test/13548957217/bb.wav '); Upload files//$ftp->move_file (' aaa/aaa.php ', ' aaa.php '); Move files//$ftp->copy_file (' aaa.php ', ' aaa/aaa.php '); Copy file//$ftp->del_file (' aaa.php '); Delete file $ftp->close (); Close the FTP connection//******************************************************************************/
3. PHP creates a directory with FTP functions
<?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 D Irectory return $newDir; } else { return false; } Ftp_close ($conn _id); Close connection } }?>
Summary: The above is the entire content of this article, I hope to be able to help you learn.