
<?php
/**
* ftp操作類
* @author chenzhouyu
*
*使用$ftps = pc_base::load_sys_class('ftps');進行初始化。
*首先通過 $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()擷取錯誤資訊
*/
class ftps {
//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)) {
}
if (@ftp_login($this->link, $username, $password)) {
if ($pasv) ftp_pasv($this->link, true);
$this->link_time = time()-$start;
return true;
$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) {
$dirname = pathinfo($remote,pathinfo_dirname);
if (!$this->chdir($dirname)) {
$this->mkdir($dirname);
if (@ftp_put($this->link, $remote, $local, $this->mode)) {
return true;
$this->err_code = 7;
* 删除檔案夾
* @param string $dirname 目錄位址
* @param boolean $enforce 強制删除
public function rmdir($dirname, $enforce = 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;
@ftp_rmdir($this->link, $dirname);
* 删除指定檔案
* @param string $filename 檔案名
public function f_delete($filename) {
if (@ftp_delete($this->link, $filename)) {
$this->err_code = 4;
* 傳回給定目錄的檔案清單
* @return array 檔案清單資料
public function nlist($dirname) {
if ($list = @ftp_nlist($this->link, $dirname)) {
return $list;
$this->err_code = 5;
* 在 ftp 伺服器上改變目前目錄
* @param string $dirname 修改伺服器上目前目錄
public function chdir($dirname) {
if (@ftp_chdir($this->link, $dirname)) {
$this->err_code = 6;
* 擷取錯誤資訊
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);
}
阿斯