1. 程式人生 > >php檔案上傳到遠端ftp伺服器程式碼封裝

php檔案上傳到遠端ftp伺服器程式碼封裝

經常在專案中遇見將檔案上傳到遠端的ftp伺服器,自己封裝了一個ftp檔案上傳類

<?php


class Ftp {
    /**
     * 上傳檔案根目錄
     * @var string
     */
    private $rootPath;

    /**
     * 本地上傳錯誤資訊
     * @var string
     */
    private $error = ''; //上傳錯誤資訊

    /**
     * FTP連線
     * @var resource
     */
    private $link;

    private $config = array(
        'host'     => '', //伺服器
        'port'     => 21, //埠
        'timeout'  => 90, //超時時間
        'username' => '', //使用者名稱
        'password' => '', //密碼
    );

    /**
     * 建構函式,用於設定上傳根路徑
     * @param array  $config FTP配置
     */
    public function __construct($config = array()){
        /* 預設FTP配置 */
        $this->config = array_merge($this->config, $config);

        /* 登入FTP伺服器 */
        if(!$this->login()){
            throw new \Exception($this->error);
        }
    }

    /**
     * 檢測上傳根目錄
     * @param string $rootpath   根目錄
     * @return boolean true-檢測通過,false-檢測失敗
     */
    public function checkRootPath($rootpath){
        /* 設定根目錄 */
        $this->rootPath = ftp_pwd($this->link) . '/' . ltrim($rootpath, '/');

        if(
[email protected]
_chdir($this->link, $this->rootPath)){             $this->error = '上傳根目錄不存在!';             return false;         }         return true;     }     /**      * 檢測上傳目錄      * @param  string $savepath 上傳目錄      * @return boolean          檢測結果,true-通過,false-失敗      */     public function checkSavePath($savepath){         /* 檢測並建立目錄 */         if (!$this->mkdir($savepath)) {             return false;         } else {             //TODO:檢測目錄是否可寫             return true;         }     }     /**      * 儲存指定檔案      * @param  array   $file    儲存的檔案資訊      * @param  boolean $replace 同名檔案是否覆蓋      * @return boolean          儲存狀態,true-成功,false-失敗      */     public function save($file, $replace=true) {         $filename = '/'.$file['rootpath'] .'/'. $file['savepath'] .'/'. $file['savename'];         //pr($filename);exit;         /* 不覆蓋同名檔案 */         // if (!$replace && is_file($filename)) {         //     $this->error = '存在同名檔案' . $file['savename'];         //     return false;         // }         /* 移動檔案 */         if (!ftp_put($this->link, $filename, $file['tmp_name'], FTP_BINARY)) {             $this->error = '檔案上傳儲存錯誤!';             return false;         }         return true;     }     /**      * 建立目錄      * @param  string $savepath 要建立的目錄      * @return boolean          建立狀態,true-成功,false-失敗      */     public function mkdir($savepath){         $dir =  $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 = "目錄 {$savepath} 建立失敗!";             return false;         }     }     /**      * 獲取最後一次上傳錯誤資訊      * @return string 錯誤資訊      */     public function getError(){         return $this->error;     }     /**      * 登入到FTP伺服器      * @return boolean true-登入成功,false-登入失敗      */     private function login(){         extract($this->config);         $this->link = ftp_connect($host, $port, $timeout);         if($this->link) {             if (ftp_login($this->link, $username, $password)) {                 ftp_pasv($this->link ,true);                return true;             } else {                 $this->error = "無法登入到FTP伺服器:username - {$username}";             }         } else {             $this->error = "無法連線到FTP伺服器:{$host}";         }         return false;     }     /**      * 析構方法,用於斷開當前FTP連線      */     public function __destruct() {         ftp_close($this->link);     } } ?> <?php //檔案呼叫 $config =array(     'ftp' => array(         'host' => '192.168.111.1',//主機名稱         'port' => 21,   //埠號         'timeout' => 90, //超時時間         'username' => 'ftp', 賬號         'password' => '123456',  密碼              ) ); $file = $_FILES['file']; $time = getTimeStamp(); $rootdir ="fireware"; $dirdate = date('Ym',$time); $fileName = $file['name']; if(!in_array(substr(strrchr($fileName, '.'), 1),array('rbin','ubin','bin'))){     $this->ajaxReturn(405405,L('lang_dev_index12')); } $config = (array)$this->config->ftp; $ftp = new Ftp($config); $rootpath = $ftp->checkRootPath($rootdir); //檢測上傳根目錄存在不存在 if(!$rootpath){     $ftp->mkdir($rootdir); } $savepath = $ftp->checkSavePath("/".$rootdir.'/'.$dirdate); //檢查上傳目錄存在不存在 if(!$savepath){     $ftp->mkdir($dirdate); } $data =array(     'rootpath'=>$rootdir,     'savepath'=>$dirdate,     'savename'=>$fileName ); $info = array_merge($file,$data); $upinfo = $ftp->save($info); if($upinfo){      //成功處理邏輯 }else{      //失敗處理邏輯 } ?>