PHP实现类似ftp操作的文件管理类,具体代码如下:
<?PHP
/**
* 仿写CodeIgniter的FTP类
* FTP基本操作:
* 1) 登陆; connect
* 2) 当前目录文件列表; filelist
* 3) 目录改变; chgdir
* 4) 重命名/移动; rename
* 5) 创建文件夹; mkdir
* 6) 删除; delete_dir/delete_file
* 7) 上传; upload
* 8) 下载 download
*
*
*/
class Ftp {
private $hostname = '';
private $username = '';
private $password = '';
private $port = 21;
private $passive = TRUE;
private $debug = TRUE;
private $conn_id = FALSE;
/**
* 构造函数
*
* @param array 配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
*/
public function __construct($config = array()) {
if(count($config) > 0) {
$this->_init($config);
}
}
/**
* FTP连接
*
* @access public
* @param array 配置数组
* @return boolean
*/
public function connect($config = array()) {
if(count($config) > 0) {
$this->_init($config);
}
if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_connect");
}
return FALSE;
}
if( ! $this->_login()) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_login");
}
return FALSE;
}
if($this->passive === TRUE) {
ftp_pasv($this->conn_id, TRUE);
}
return TRUE;
}
/**
* 目录改变
*
* @access public
* @param string 目录标识(ftp)
* @param boolean
* @return boolean
*/
public function chgdir($path = '', $supress_debug = FALSE) {
if($path == '' OR ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_chdir($this->conn_id, $path);
if($result === FALSE) {
if($this->debug === TRUE AND $supress_debug == FALSE) {
$this->_error("ftp_unable_to_chgdir:dir[".$path."]");
}
return FALSE;
}
return TRUE;
}
/**
* 目录生成
*
* @access public
* @param string 目录标识(ftp)
* @param int 文件权限列表
* @return boolean
*/
public function mkdir($path = '', $permissions = NULL) {
if($path == '' OR ! $this->_isconn()) {
return FALSE;
}
$result = @ftp_mkdir($this->conn_id, $path);
if($result === FALSE) {
if($this->debug === TRUE) {
$this->_error("ftp_unable_to_mkdir:dir[".$path."]");
}
return FALSE;
} 转载请注明诚信在线娱乐http://www.shall-market.com