FTP操作類---拷貝、移動、刪除檔案/建立目錄

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   for   檔案   on   div   

FTP操作類---拷貝、移動、刪除檔案/建立目錄

<?php/*** 作用:FTP操作類( 拷貝、移動、刪除檔案/建立目錄 )* 時間:2006/5/9* 欣然隨風* QQ:276624915*/class class_ftp{     public $off; // 返回操作狀態(成功/失敗)    public $conn_id; // FTP串連     /**      * 方法:FTP串連      * @FTP_HOST -- FTP主機      * @FTP_PORT -- 連接埠      * @FTP_USER -- 使用者名稱      * @FTP_PASS -- 密碼      */    function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)     {        $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP伺服器串連失敗");         @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP伺服器登陸失敗");         @ftp_pasv($this->conn_id,1); // 開啟被動類比    }    /**      * 方法:上傳檔案      * @path -- 本地路徑      * @newpath -- 上傳路徑      * @type -- 若目標目錄不存在則建立      */    function 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 "檔案上傳失敗,請檢查許可權及路徑是否正確!";     }    /**      * 方法:移動檔案      * @path -- 原路徑      * @newpath -- 新路徑      * @type -- 若目標目錄不存在則建立      */    function move_file($path,$newpath,$type=true)     {         if($type) $this->dir_mkdirs($newpath);        $this->off = @ftp_rename($this->conn_id,$path,$newpath);         if(!$this->off) echo "檔案移動失敗,請檢查許可權及原路徑是否正確!";     }    /**      * 方法:複製檔案      * 說明:由於FTP無複製命令,本方法變通操作為:下載後再上傳到新的路徑      * @path -- 原路徑      * @newpath -- 新路徑      * @type -- 若目標目錄不存在則建立      */    function copy_file($path,$newpath,$type=true)     {        $downpath = "c:/tmp.dat";        $this->off = @ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);// 下載        if(!$this->off) echo "檔案複製失敗,請檢查許可權及原路徑是否正確!";        $this->up_file($downpath,$newpath,$type);     }    /**      * 方法:刪除檔案      * @path -- 路徑      */    function del_file($path)     {        $this->off = @ftp_delete($this->conn_id,$path);         if(!$this->off) echo "檔案刪除失敗,請檢查許可權及路徑是否正確!";     }    /**      * 方法:組建目錄      * @path -- 路徑      */    function dir_mkdirs($path)     {        $path_arr = explode(‘/‘,$path); // 取目錄數組        $file_name = array_pop($path_arr); // 彈出檔案名稱        $path_div = count($path_arr); // 取層數        foreach($path_arr as $val) // 建立目錄        {             if(@ftp_chdir($this->conn_id,$val) == FALSE)             {                $tmp = @ftp_mkdir($this->conn_id,$val);                 if($tmp == FALSE)                 {                     echo "目錄建立失敗,請檢查許可權及路徑是否正確!";                     exit;                 }                 @ftp_chdir($this->conn_id,$val);             }         }         for($i=1;$i<=$path_div;$i++) // 回退到根        {             @ftp_cdup($this->conn_id);         }     }    /**      * 方法:關閉FTP串連      */    function close()     {         @ftp_close($this->conn_id);     }}// class class_ftp end/************************************** 測試 ***********************************$ftp = new class_ftp(‘192.168.100.143‘,21,‘user‘,‘pwd‘); // 開啟FTP串連//$ftp->up_file(‘aa.txt‘,‘a/b/c/cc.txt‘); // 上傳檔案//$ftp->move_file(‘a/b/c/cc.txt‘,‘a/cc.txt‘); // 移動檔案//$ftp->copy_file(‘a/cc.txt‘,‘a/b/dd.txt‘); // 複製檔案//$ftp->del_file(‘a/b/dd.txt‘); // 刪除檔案$ftp->close(); // 關閉FTP串連******************************************************************************/?>

  詳細說明:http://php.662p.com/thread-548-1-1.html

FTP操作類---拷貝、移動、刪除檔案/建立目錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.