FTP操作類---拷貝、移動(dòng)、刪除文件/創(chuàng)建目錄
<?php/*** 作用:FTP操作類( 拷貝、移動(dòng)、刪除文件/創(chuàng)建目錄 )* 時(shí)間:2006/5/9* 作者:欣然隨風(fēng)* QQ:276624915*/class class_ftp{ public $off; // 返回操作狀態(tài)(成功/失敗) public $conn_id; // FTP連接 /** * 方法:FTP連接 * @FTP_HOST -- FTP主機(jī) * @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服務(wù)器連接失敗"); @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP服務(wù)器登陸失敗"); @ftp_pasv($this->conn_id,1); // 打開被動(dòng)模擬 } /** * 方法:上傳文件 * @path -- 本地路徑 * @newpath -- 上傳路徑 * @type -- 若目標(biāo)目錄不存在則新建 */ 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 "文件上傳失敗,請(qǐng)檢查權(quán)限及路徑是否正確!"; } /** * 方法:移動(dòng)文件 * @path -- 原路徑 * @newpath -- 新路徑 * @type -- 若目標(biāo)目錄不存在則新建 */ 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 "文件移動(dòng)失敗,請(qǐng)檢查權(quán)限及原路徑是否正確!"; } /** * 方法:復(fù)制文件 * 說明:由于FTP無復(fù)制命令,本方法變通操作為:下載后再上傳到新的路徑 * @path -- 原路徑 * @newpath -- 新路徑 * @type -- 若目標(biāo)目錄不存在則新建 */ 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 "文件復(fù)制失敗,請(qǐng)檢查權(quán)限及原路徑是否正確!"; $this->up_file($downpath,$newpath,$type); } /** * 方法:刪除文件 * @path -- 路徑 */ function del_file($path) { $this->off = @ftp_delete($this->conn_id,$path); if(!$this->off) echo "文件刪除失敗,請(qǐng)檢查權(quán)限及路徑是否正確!"; } /** * 方法:生成目錄 * @path -- 路徑 */ function dir_mkdirs($path) { $path_arr = explode('/',$path); // 取目錄數(shù)組 $file_name = array_pop($path_arr); // 彈出文件名 $path_div = count($path_arr); // 取層數(shù) foreach($path_arr as $val) // 創(chuàng)建目錄 { if(@ftp_chdir($this->conn_id,$val) == FALSE) { $tmp = @ftp_mkdir($this->conn_id,$val); if($tmp == FALSE) { echo "目錄創(chuàng)建失敗,請(qǐng)檢查權(quán)限及路徑是否正確!"; exit; } @ftp_chdir($this->conn_id,$val); } } for($i=1;$i<=$path_div;$i++) // 回退到根 { @ftp_cdup($this->conn_id); } } /** * 方法:關(guān)閉FTP連接 */ function close() { @ftp_close($this->conn_id); }}// class class_ftp end/************************************** 測(cè)試 ***********************************$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'); // 移動(dòng)文件//$ftp->copy_file('a/cc.txt','a/b/dd.txt'); // 復(fù)制文件//$ftp->del_file('a/b/dd.txt'); // 刪除文件$ftp->close(); // 關(guān)閉FTP連接******************************************************************************/?>
詳細(xì)說明:http://php.662p.com/thread-548-1-1.html
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注