標籤:des io ar os sp 檔案 on log cti
php具有很好的擴充性,在php中對於ftp檔案的上傳也是內建功能,和其他語言一樣,只能實現的是用戶端。
class FtpClient { /** * 上傳檔案根目錄 * @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){ /* 預設FTP配置 */ $this->config = array_merge($this->config, $config); /* 登入FTP伺服器 */ if(!$this->login()){ E($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 = $this->rootPath . $file[‘savepath‘] . $file[‘savename‘]; /* 不覆蓋同名檔案 */ // 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 = $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 = "目錄 {$savepath} 建立失敗!"; return false; } } /** * 建立目錄 * @param string $file 目標檔案或者目錄 * @return real 檔案大小或者-1 */ public function filesize($file) { return @ftp_size($this->link,$file); } /** * 擷取最後一次上傳錯誤資訊 * @return string 錯誤資訊 */ public function getError(){ return $this->error; } /** * 給檔案或者目錄授權 * @param String $file 檔案或者路徑 * @param $mode 八進位許可權值 1- 執行許可權,2-寫入權限,4 - 讀許可權 0644->所有者可讀寫,其他人可讀 * @return 設定成功的新許可權或者失敗時false */ public function chmod($file,$mode) { if(!ftp_chmod(($this->link,$mode,$file)) { $this->error = ‘授權失敗‘; } } /** * @param string path 必需。規定要刪除的檔案的路徑 * @return true or false */ public function delete($path) { if (!ftp_delete($this->link, $path)) { $this->error =‘刪除檔案:‘.$path.‘ 失敗‘; } } /** * @param string local 必需。本地檔案儲存體路徑 * @param string remote 必需。遠程檔案路徑 * @param string mode 必需。讀模數式 * @param string resume 讀取檔案大小的起始位置 * @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 =‘讀取遠程檔案:‘.$remote.‘ 失敗‘; } } /** * 登入到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)) { return true; } else { $this->error = "無法登入到FTP伺服器:username - {$username}"; } } else { $this->error = "無法串連到FTP伺服器:{$host}"; } return false; } /** * 析構方法,用於斷開當前FTP串連 */ public function __destruct() { ftp_close($this->link); }}
try doing it!
PHP構建FTP用戶端