PHP遠程附件上傳

來源:互聯網
上載者:User

 

今天對ShuipFCMS程式增加了遠程附件的功能。是利用FTP實現,下面貼出一個PHPCMS V9裡面的一個FTP處理類,大致講講怎麼實現遠程附件的實現。

FTP類原始碼:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 class Ftp {            //FTP 串連資源    private $link;    //FTP連線時間    public $link_time;    //錯誤碼    private $err_code = 0;    //傳送模式{文字模式:FTP_ASCII, 二進位模式:FTP_BINARY}    public $mode = FTP_BINARY;            /**     * 串連FTP伺服器     * @param string $host       伺服器位址     * @param string $username   使用者名稱     * @param string $password   密碼     * @param integer $port       伺服器連接埠,預設值為21     * @param boolean $pasv        是否開啟被動模式     * @param boolean $ssl      是否使用SSL串連     * @param integer $timeout     逾時時間      */    public function connect($host, $username = '', $password = '', $port = '21', $pasv = false, $ssl = false, $timeout = 30) {        $start = time();        if ($ssl) {            if (!$this->link = @ftp_ssl_connect($host, $port, $timeout)) {                $this->err_code = 1;                return false;            }        } else {            if (!$this->link = @ftp_connect($host, $port, $timeout)) {                $this->err_code = 1;                return false;            }        }                if (@ftp_login($this->link, $username, $password)) {            if ($pasv)                ftp_pasv($this->link, true);            $this->link_time = time() - $start;            return true;        } else {            $this->err_code = 1;            return false;        }        register_shutdown_function(array(&$this, 'close'));    }            /**     * 建立檔案夾     * @param string $dirname 目錄名,     */    public function mkdir($dirname) {        if (!$this->link) {            $this->err_code = 2;            return false;        }        $dirname = $this->ck_dirname($dirname);        $nowdir = '/';        foreach ($dirname as $v) {            if ($v && !$this->chdir($nowdir . $v)) {                if ($nowdir)                    $this->chdir($nowdir);                @ftp_mkdir($this->link, $v);            }            if ($v)                $nowdir .= $v . '/';        }        return true;    }            /**     * 上傳檔案     * @param string $remote 遠程存放地址     * @param string $local 本地存放地址     */    public function put($remote, $local) {        if (!$this->link) {            $this->err_code = 2;            return false;        }        $dirname = pathinfo($remote, PATHINFO_DIRNAME);        if (!$this->chdir($dirname)) {            $this->mkdir($dirname);        }        if (@ftp_put($this->link, $remote, $local, $this->mode)) {            return true;        } else {            $this->err_code = 7;            return false;        }    }            /**     * 刪除檔案夾     * @param string $dirname  目錄位址     * @param boolean $enforce 強制移除     */    public function rmdir($dirname, $enforce = false) {        if (!$this->link) {            $this->err_code = 2;            return false;        }        $list = $this->nlist($dirname);        if ($list && $enforce) {            $this->chdir($dirname);            foreach ($list as $v) {                $this->f_delete($v);            }        } elseif ($list && !$enforce) {            $this->err_code = 3;            return false;        }        @ftp_rmdir($this->link, $dirname);        return true;    }            /**     * 刪除指定檔案     * @param string $filename 檔案名稱     */    public function f_delete($filename) {        if (!$this->link) {            $this->err_code = 2;            return false;        }        if (@ftp_delete($this->link, $filename)) {            return true;        } else {            $this->err_code = 4;            return false;        }    }            /**     * 返回給定目錄的檔案清單     * @param string $dirname  目錄位址     * @return array 檔案清單資料     */    public function nlist($dirname) {        if (!$this->link) {            $this->err_code = 2;            return false;        }        if ($list = @ftp_nlist($this->link, $dirname)) {            return $list;        } else {            $this->err_code = 5;            return false;        }    }            /**     * 在 FTP 伺服器上改變目前的目錄     * @param string $dirname 修改伺服器上目前的目錄     */    public function chdir($dirname) {        if (!$this->link) {            $this->err_code = 2;            return false;        }        if (@ftp_chdir($this->link, $dirname)) {            return true;        } else {            $this->err_code = 6;            return false;        }    }            /**     * 擷取錯誤資訊     */    public function get_error() {        if (!$this->err_code)            return false;        $err_msg = array(            '1' => 'Server can not connect',            '2' => 'Not connect to server',            '3' => 'Can not delete non-empty folder',            '4' => 'Can not delete file',            '5' => 'Can not get file list',            '6' => 'Can not change the current directory on the server',            '7' => 'Can not upload files'        );        return $err_msg[$this->err_code];    }            /**     * 檢測目錄名     * @param string $url 目錄     * @return 由 / 分開的返回數組     */    private function ck_dirname($url) {        $url = str_replace('', '/', $url);        $urls = explode('/', $url);        return $urls;    }            /**     * 關閉FTP串連     */    public function close() {        return @ftp_close($this->link);    }        }

 

先來說說遠程附件上傳的大致流程:

   使用者選擇檔案上傳提交到伺服器->伺服器接收到檔案->伺服器一些安全檢測完成通過FTP功能上傳到相應FTP伺服器。

我說的只是一個大概過程,不是很標準,明白個意思即可啦!~

這個類大致使用方法:

  首先通過 $ftps->connect($host,$username,$password,$post,$pasv,$ssl,$timeout);進行FTP伺服器串連。

  通過具體的函數進行FTP的操作。

   $ftps->mkdir() 建立目錄,可以建立多級目錄以“/abc/def/higk”的形式進行多級目錄的建立。

   $ftps->put()上傳檔案

   $ftps->rmdir()刪除目錄

   $ftps->f_delete()刪除檔案

   $ftps->nlist()列出指定目錄的檔案

   $ftps->chdir()變更當前檔案夾

   $ftps->get_error()擷取錯誤資訊



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.