PHP use FTP remote upload file class (Perfect solution of master-Slave file synchronization problem method) _php skills

Source: Internet
Author: User
Tags create directory ftp upload file mixed

PHP uses FTP to implement file upload code snippets:

<?php/** * FTP upload File class */class FTP {/** * Test server * * @var array/private $testServer = Array (' Host ' => ' IP ', ' Port ' =>, ' user ' => ' userName ', ' pwd ' => 

    ' Password ');       /** * Open and Log on server * * @param string $flag Server identification Test * @return Mixed * 0: Server Connection Failed * 1: Server Login failed * Resource Connection ID */Public Function openserver ($flag = ' Test ') {//Select server $conf 

      IG = $this->getserverconfig ($flag); 
      Connection Server $connect = Ftp_connect ($config [' Host '], $config [' Port ']); 

      if ($connect = = false) return 0; 

      Login Server if (!ftp_login ($connect, $config [' User '], $config [' pwd ']) return 1; 

      Turn on Passive mode, and the transfer of data is initiated by the client rather than the server starting FTP_PASV ($connect, true); 
    Returns the connection identification return $connect;
    /** * Creates the directory and navigates the directory to the directory * @param resource $connect Connection ID * @param string $dirPath directory path * @return Mixed * 2: Create directory Failed * True: Create directory Success/Public function MakeDir ($connect, $dirPath) { Process directory $dirPath = '/'. 
      Trim ($dirPath, '/'); 
      $dirPath = explode ('/', $dirPath); 
        foreach ($dirPath as $dir) {if ($dir = = ') $dir = '/'; To determine if the directory exists if (@ftp_chdir ($connect, $dir) = = False) {//To determine if the directory created a successful if (@ftp_mkDir $connect, $d 
          IR) = = False) {return 2; 
        @ftp_chdir ($connect, $dir); 
    } return true; 
      /** * Shutdown Server * * @param resource $connect connection ID/Public function closeserver ($connect) { 
    if (!empty ($connect)) ftp_close ($connect); /** * Upload File * * @param string $flag Server identification * @param string $local The local path of the uploaded file * @param strin       G $remote remote Path to upload file * @return int * 0: Server Connection Failed * 1: Server Login failed * 2: Failed to create directory *    3: Failed to upload file *   4: Upload success/Public function upload ($flag = ' Test ', $local, $remote) {//Connect and login to server $connect = $this- 
      >openserver ($flag); if (($connect = = 0) | | ($connect = = 1)) 

      return $connect; 
      Upload file directory processing $MDR = $this->makedir ($connect, DirName ($remote)); 

      if ($MDR = = 2) return 2; 

      Upload file $result = ftp_put ($connect, basename ($remote), $local, ftp_binary); 

      Shut down the server $this->closeserver ($connect); Returns the result return (! $result)? 
    3:4; 
     /** * Delete File * * @param string $flag Server Identity * @param string $remote remote path of file * @return int * 0: Server Connection Failed * 1: Server Login failed * 2: Delete failed * 3: Delete successful/public function delete ($flag = ' Test ', $remote) 
      {//Connect and log in to the server $connect = $this->openserver ($flag); if (($connect = = 0) | | ($connect = = 1)) 

      return $connect; 

      Delete $result = Ftp_delete ($connect, $remote); Shutting down the server
      $this->closeserver ($connect); Returns the result return (! $result)? 
    2:3;  
     /** * Read File * * @param string $flag Server identification * @param string $remote The remote path of the file * @return Mixed 
      * 0: Server Connection Failed * 1: Server login failed/Public function read ($flag, $remote) {//Connect and log on to server 
      $connect = $this->openserver ($flag); if (($connect = = 0) | | ($connect = = 1)) 

      return $connect; 

      Read $result = Ftp_nlist ($connect, $remote); 

      Shut down the server $this->closeserver ($connect); Returns the result foreach ($result as $key => $value) {if In_array ($value, Array ('. ', ' ... ')) unset ($result [$key]) 
      ; 
    Return Array_values ($result); /** * Get FTP Server Configuration * * @param string $flag server Identity Test * @return Array FTP Server Connection configuration * * PR 
      ivate function getserverconfig ($flag = ' Test ') {$flag = Strtolower ($flag); Test server if ($flag = = ' Test ') return $this->testserver; 
    Default back to test server return $this->testserver; }}?>

The above is a small series for everyone to bring PHP using FTP remote Upload file class (perfect solution to the problem of master-Slave file synchronization method) of all the content, I hope to help you, a lot of support cloud Habitat Community ~

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.