PHP Build FTP client

Source: Internet
Author: User
Tags ftp connection ftp file ftp client

PHP has a good extensibility, in PHP for FTP file Upload is also built-in functionality, and other languages, can only be implemented by the client.

class ftpclient {   /**     *  upload file root directory       *  @var  string     */    private $ rootpath;    /**     *  Local upload error message       *  @var  string     */    private  $error   =  ';  //upload error message     /**     * ftp connection       *  @var  resource     */    private   $link;    private  $config  = array (          ' host '      =>  ',  //server           ' Port '      => 21, //port           ' Timeout '   => 90, //timeout Time          ' username '  =>  ',  //username          ' password '  =>  ',  //password      );    /**     *  constructor for setting the upload root path       *  @param  array   $config  ftp configuration      */     public function __construct ($config) {         /*  Default FTP configuration  */         $this->config =  Array_merge ($this->config,  $config);        /*  Log on to the FTP server  */        if (! $this->login ()) {             e ($this->error);         }     }    /**     *  detect upload root      *  @param  string  $rootpath     root      *  @return  boolean  true-detection passed, false-detection failure      */    public function  Checkrootpath ($rootpath) {        /*  set the root directory  */          $this->rootpath = ftp_pwd ($this->link)  .  '/'  . ltrim ($rootpath,  '/');         if ([email  Protected]_chdir ($this->link,  $this->rootpath)) {              $this->error =  ' upload root directory does not exist! ';            return false;         }        return true;    }    /**     *  Detection Upload Directory      *  @param   string  $savepath   Upload directory       *  @return  boolean           test results, true-Pass, false-Failure      */    public function checksavepath ($ Savepath) {        /*  Detect and create catalogs  */         if  (! $this->mkdir ($savepath))  {             return false;        } else  {            //todo: Check whether the catalogue is writable              return true;         }    }    /**     *  Save the specified file      *   @param   array    $file      saved file information       *  @param   boolean  $replace   If the file with the same name overrides      *  @return  boolean           Save status, true-success, false-failure       */    public function save ($file,  $replace =true)  {          $filename  =  $this->rootpath .  $file [' Savepath '] .  $file [' savename '];        /*  do not overwrite files of the same name   */        // if  (! $replace  && is_file ($ FileName)  {        //      $this error =  ' exists file with the same name '  .   $file [' Savename '];        //     return  false;        // }         /*  Mobile File  */        if  (!ftp_put ($this link,  $filename,  $file [' Tmp_name '], ftp_binary)  {              $this->error =  ' File Upload Save error! ';            return false;         }        return true;     }    /**     *  Create a catalog       *  @param   string  $savepath   Muri      *  @return to create  boolean           created status, True-succeeded, false-failed      */    public function  mkdir ($savepath) {         $dir  =  $this->rootpath  .  $savepath         if (Ftp_chdir ($this->link,  $dir) ) {            return true;         }        if (Ftp_mkdir ($this->link,   $dir)) {            return true;         } elseif ($this->mkdir (dirname ($savepath))  &&  ftp_mkdir ($this->link,  $dir))  {             return true;        } else {              $this->error =  "directory  {$savepath}  creation failed! ";            return false;         }    }    /**      *  Create a catalog      *  @param   string  $file        target files or directories      *  @return  real            File size or -1     */    public function  filesize ($file)     {        return  @ftp _size ($this->link, $file);    }    /**      *  get the last upload error message      *  @return  string  error message       */    publiC function geterror () {        return  $this->error;     }    /**     *  license to file or directory       *  @param  String  $file   file or path      *  @param   $mode   Octal privilege value  1-  Execute permission, 2-write permission,4 -  Read permission   0644-> owner read/write, others readable       *  @return   Set new permissions for success or fail false     */     public function chmod ($file, $mode)     {         if (!ftp_chmod ($this->link, $mode, $file))         {              $this->error =  ' authorization failed ';         }    }    /**      *&nbSP; @param  string  path   required. Specifies the path of the file to be deleted      *  @return  true or false       */    public function delete ($path)     {         if  (!ftp_delete ($this->link,  $path))  {             $this->error = ' delete file: '. $path. '   Failure ';         }    }    /* *     *  @param  string local   required. Local file storage path      *  @param  string remote   required. Remote file path      *  @param  string mode   required. Read mode      *  @param  string resume   start location of read file size       *  @return  true or fAlse      */    public function fetch ($local, $ Remote, $mode, $resume =0)     {         $arr _mode  = array (ftp_ascii,ftp_binary);         if (!in_array ($mode,   $arr _mode))         {              $mode  = ftp_binary;        }         if (!ftp_get ($this->link, $local, $remote, $mode, $resume))         {              $this->error = ' read remote files: '. $remote. '   Failure ';         }    }    /* *     *  Log on to the FTP server      *  @return boolean true-Login successful, false-Login failed      */    private  function login () {        extract ($this->config);          $this->link = ftp_connect ($host,  $port,  $ Timeout);         if ($this->link)  {             if  (Ftp_login ($this->link,  $username,  $ Password))  {                return true;            } else {                  $this error =  "Unable to log on to FTP server: username - {$username}";             }        } else {              $this->error =  "Unable to connect to FTP server: {$host}";         }        return false;    }     /**     *  destructor to disconnect the current FTP connection      */     public function __destruct ()  {         ftp_close ($this->link);     }}



Try doing it!

PHP Build FTP client

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.