PHP Operation FTP class (upload, download, move, create, etc.) _php skills

Source: Internet
Author: User
Tags create directory ftp connection learn php learn php programming parent directory php programming zip

This article for PHP operation FTP class for detailed introduction, PHP implementation of FTP upload, FTP download, FTP Mobile, FTP creation, etc. for your reference, the specific content as follows

1. Use PHP to manipulate ftp-usage

<?php//Join FTP Server $conn = Ftp_connect (ftp.server.com); 
  
Use username and password login ftp_login ($conn, "John", "Doe"); 
  
Get the remote system type Ftp_systype ($conn); 
  
Listing $filelist = ftp_nlist ($conn, "."); 
  
Download file Ftp_get ($conn, "Data.zip", "Data.zip", ftp_binary); 
  
Close Join Ftp_quit ($conn); Initial knot an FTP join, PHP provides ftp_connect () This function, it uses the host name and port as parameters. 
  
In the example above, 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. 
Ftp_connect () returns a handle handle after the join succeeds, and this handle will be used by the FTP function used later. 
  
$conn = Ftp_connect (ftp.server.com); Once a 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 committed to the correct server. 
  
Ftp_login ($conn, "John", "Doe"); 
  
Close connection ftp_quit ($conn); 
Logged on to the FTP server, PHP provides functions that can get information about systems 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); 
  
The passive mode (PASV) is switched on or off PASV (1 = open) FTP_PASV ($conn, 1); 
Enter the directory with the Ftp_chdir () function, which accepts a directory name as an argument. Ftp_chdir ($conn, "PubliC_html "); 
  
Return to the directory parent directory with Ftp_cdup () to implement Ftp_cdup ($conn); 
Create or move a directory using the Ftp_mkdir () and Ftp_rmdir () functions; note: if Ftp_mkdir () is successful, it will return the newly created directory name. 
  
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, uploaded file name and the type of transmission. 
  
For example: If you want to upload "abc.txt" This file, after uploading named "Xyz.txt", the order should be this: Ftp_put ($conn, "Xyz.txt", "Abc.txt", ftp_ascii); Download file: PHP provides the function is Ftp_get (), it also requires a server file name, download the file name, as well as transport type as parameters, such as: Server-side files 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 the file name and directory, and the other is to specify the file size, permissions, time of creation, and so on. 
The first type uses the Ftp_nlist () function, and the second uses the Ftp_rawlist (). Two functions require a directory name as a parameter, which returns a catalog column as an array, each of which corresponds to a row of the list. 
  
$filelist = Ftp_nlist ($conn, "."); function Ftp_size (), which returns the size of the file you specified, using bites as the unit. 
  
To point out, if it returns "-1", it means that it is a directory $filelist = ftp_size ($conn, "data.zip"); 
 ?>

 2. FTP upload Class (ftp.php)

<?php/******************************************** * Module:ftp class *******************************************/             Class FTP {public $off;           Returns the Operation state (success/failure) 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"); 
    @ftp_login ($this->conn_id, $FTP _user, $FTP _pass) or Die ("FTP server login failed"); @ftp_pasv ($this->conn_id,1); Open Passive Simulation}/** * Method: Upload file * @path-Local Path * @newpath--Upload path * @type-if the target directory does not exist then new * * FUNCTI 
    On 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, please check the permissions and the path is correct!" 
  "; /** * Method: Move File * @path--Original path * @newpath --New Path * @type-NEW */function Move_file ($path, $newpath, $type =true) {if ($type) $this If the destination directory does not exist->di 
    R_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: Because FTP no replication command, this method workaround: Download and then upload to the new path * @path-the original path * @newpath-New path * @type- 
    The new */function Copy_file ($path, $newpath, $type =true) {$downpath = "C:/tmp.dat", if the label directory does not exist; $this->off = @ftp_get ($this->conn_id, $downpath, $path, ftp_binary)//download if (! $this->off) echo File replication failed, please check the permissions and the original The path is correct! 
    "; 
  $this->up_file ($downpath, $newpath, $type); /** * Method: Delete file * @path-path/function Del_file ($path) {$this->off = @ftp_delete ($this-&G 
    t;conn_id, $path); if (! $this->off) echo file deletion failed, please check that the permissions and path are correct! 
  ";      /** * Method: Generate directory * @path-path/function Dir_mkdirs ($path) {$path _arr = explode ('/', $path); Directory array $file _name = Array_pop ($path _arr);        Pop-up filename $path _div = count ($path _arr); 
      Take a 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 the path is correct! 
          "; 
        Exit 
      @ftp_chdir ($this->conn_id, $val); 
    For ($i =1 $i <= $path _div $i + +)//rollback to root {@ftp_cdup ($this->conn_id); 
  }/** * Method: Turn off 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 the FTP connection 
$ftp->up_file (' aa.wav ', ' test/13548957217/bb.wav ');     Upload file 
//$ftp->move_file (' aaa/aaa.php ', ' aaa.php ');        Move file 
//$ftp->copy_file (' aaa.php ', ' aaa/aaa.php ');        Copy file 
//$ftp->del_file (' aaa.php ');                Delete file 
$ftp->close ();                       Turn off the FTP connection 
//******************************************************************************/ 

3. PHP Create directory with FTP function

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

The above is the entire content of this article, I hope that you learn PHP programming help.

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.