php ftp類
來源:互聯網
上載者:User
ftp_server = server; $this->ftp_user = username; $this->ftp_pass = password; $this->ftp_port = port; // 建立串連 $this->conn_id = ftp_connect($this->ftp_server, $this->ftp_port) or die("不能夠串連到 $this->ftp_server"); // 嘗試登陸 if (!ftp_login($this->conn_id, $this->ftp_user, $this->ftp_pass)) { $this->message("串連失敗 $this->ftp_user"); } else { $this->message("串連成功 $this->ftp_user "); } } //功能:建立新的目錄 //$path預設為空白目錄 //建立成功返回true,否則返回false。 function newdir($path = null) { if($this->ftp_is_dir($this->conn_id,$path)||@ftp_mkdir($this->conn_id,$path)) return true; if(!$this->newdir(dirname($path))) return false; ftp_mkdir($this->conn_id,$path); return true; } //驗證是否為目錄 //對$path進行驗證:如果是目錄返回true,否則返回false。 function ftp_is_dir($path) { $original_directory = ftp_pwd($this->conn_id); if(@ftp_chdir($this->conn_id,$path)) { ftp_chdir($this->conn_id,$original_directory); return true; } else return false; } //功能:上傳檔案 //$ftppath:存在ftp伺服器位置;$localpath:本地檔案位置; //上傳成功返回true,否則返回false。 function uploadfile($ftppath = null, $localpath = null) { if(!empty($ftppath) && !empty($localpath)) { $ret = ftp_nb_put($this->conn_id, $ftppath, $localpath, FTP_BINARY); while ($ret == FTP_MOREDATA) { $ret = ftp_nb_continue ($this->conn_id); } if ($ret != FTP_FINISHED) { $this->message( "上傳失敗"); return false; } else { $this->message("上傳成功"); return true; } } } //功能:刪除目錄 //$dir:要刪除的目錄 //刪除成功返回true,否則返回false。 function deldir($dir = null) { if (ftp_rmdir($this->conn_id, $dir)) { $this->message("刪除目錄成功"); return true; } else { $this->message("刪除目錄失敗"); return false; } } //功能:返回目錄 //返回目前的目錄名稱 function redir() { return ftp_pwd($this->conn_id); } //功能:刪除檔案 //$path:檔案路徑 //刪除成功返回true,否則返回false。 function delfile($path = null) { if(ftp_delete($this->conn_id, $path)) { $this->message("刪除檔案成功"); return true; } else { $this->message("刪除檔案失敗"); return false; } } //功能:列印資訊 //$str:要列印的資訊 function message($str = null) { if(!empty($str)) { echo $str; } } //功能:關閉ftp串連 function closeftp() { ftp_close($this->conn_id); } } /* 一下為示範; redir(); $ftppath='/test'; $locpath="/home/liye/public_html/php_ftp/test"; //$conn->uploadfile($ftppath,$locpath); //$conn->newdir('test/123/1233'); //$conn->deldir('test/123/1233'); //$conn->delfile($ftppath); $conn->delfile('ftp.php'); ?> */