Home >php教程 >php手册 >仿写CodeIgniter的FTP操作类

仿写CodeIgniter的FTP操作类

WBOY
WBOYOriginal
2016-06-07 11:41:59987browse

仿写CodeIgniter的FTP操作类
<?PHP <br /> /**<br>  * 仿写CodeIgniter的FTP类<br>  * FTP基本操作:<br>  * 1) 登陆;             connect<br>  * 2) 当前目录文件列表;  filelist<br>  * 3) 目录改变;            chgdir<br>  * 4) 重命名/移动;        rename<br>  * 5) 创建文件夹;        mkdir<br>  * 6) 删除;                delete_dir/delete_file<br>  * 7) 上传;                upload<br>  * 8) 下载                download<br>  *<br>  * <br>  */<br> class Ftp {<br> <br>     private $hostname    = '';<br>     private $username    = '';<br>     private $password    = '';<br>     private $port         = 21;<br>     private $passive     = TRUE;<br>     private $debug        = TRUE;<br>     private $conn_id     = FALSE;<br>     <br>     /**<br>      * 构造函数<br>      *<br>      * @param    array    配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);<br>      */<br>     public function __construct($config = array()) {<br>         if(count($config) > 0) {<br>             $this->_init($config);<br>         }<br>     }<br>     <br>     /**<br>      * FTP连接<br>      *<br>      * @access     public<br>      * @param     array     配置数组<br>      * @return    boolean<br>      */<br>     public function connect($config = array()) {<br>         if(count($config) > 0) {<br>             $this->_init($config);<br>         }<br>         <br>         if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_connect");<br>             }<br>             return FALSE;<br>         }<br>         <br>         if( ! $this->_login()) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_login");<br>             }<br>             return FALSE;<br>         }<br>         <br>         if($this->passive === TRUE) {<br>             ftp_pasv($this->conn_id, TRUE);<br>         }<br>         <br>         return TRUE;<br>     }<br> <br>     <br>     /**<br>      * 目录改变<br>      *<br>      * @access     public<br>      * @param     string     目录标识(ftp)<br>      * @param    boolean    <br>      * @return    boolean<br>      */<br>     public function chgdir($path = '', $supress_debug = FALSE) {<br>         if($path == '' OR ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         $result = @ftp_chdir($this->conn_id, $path);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE AND $supress_debug == FALSE) {<br>                 $this->_error("ftp_unable_to_chgdir:dir[".$path."]");<br>             }<br>             return FALSE;<br>         }<br>         <br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 目录生成<br>      *<br>      * @access     public<br>      * @param     string     目录标识(ftp)<br>      * @param    int      文件权限列表    <br>      * @return    boolean<br>      */<br>     public function mkdir($path = '', $permissions = NULL) {<br>         if($path == '' OR ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         $result = @ftp_mkdir($this->conn_id, $path);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_mkdir:dir[".$path."]");<br>             }<br>             return FALSE;<br>         }<br>         <br>         if( ! is_null($permissions)) {<br>             $this->chmod($path,(int)$permissions);<br>         }<br>         <br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 上传<br>      *<br>      * @access     public<br>      * @param     string     本地目录标识<br>      * @param    string    远程目录标识(ftp)<br>      * @param    string    上传模式 auto || ascii<br>      * @param    int        上传后的文件权限列表    <br>      * @return    boolean<br>      */<br>     public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         if( ! file_exists($localpath)) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_no_source_file:".$localpath);<br>             }<br>             return FALSE;<br>         }<br>         <br>         if($mode == 'auto') {<br>             $ext = $this->_getext($localpath);<br>             $mode = $this->_settype($ext);<br>         }<br>         <br>         $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;<br>         <br>         $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");<br>             }<br>             return FALSE;<br>         }<br>         <br>         if( ! is_null($permissions)) {<br>             $this->chmod($remotepath,(int)$permissions);<br>         }<br>         <br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 下载<br>      *<br>      * @access     public<br>      * @param     string     远程目录标识(ftp)<br>      * @param    string    本地目录标识<br>      * @param    string    下载模式 auto || ascii    <br>      * @return    boolean<br>      */<br>     public function download($remotepath, $localpath, $mode = 'auto') {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         if($mode == 'auto') {<br>             $ext = $this->_getext($remotepath);<br>             $mode = $this->_settype($ext);<br>         }<br>         <br>         $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;<br>         <br>         $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");<br>             }<br>             return FALSE;<br>         }<br>         <br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 重命名/移动<br>      *<br>      * @access     public<br>      * @param     string     远程目录标识(ftp)<br>      * @param    string    新目录标识<br>      * @param    boolean    判断是重命名(FALSE)还是移动(TRUE)    <br>      * @return    boolean<br>      */<br>     public function rename($oldname, $newname, $move = FALSE) {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         $result = @ftp_rename($this->conn_id, $oldname, $newname);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE) {<br>                 $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";<br>                 $this->_error($msg);<br>             }<br>             return FALSE;<br>         }<br>         <br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 删除文件<br>      *<br>      * @access     public<br>      * @param     string     文件标识(ftp)<br>      * @return    boolean<br>      */<br>     public function delete_file($file) {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         $result = @ftp_delete($this->conn_id, $file);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_delete_file:file[".$file."]");<br>             }<br>             return FALSE;<br>         }<br>         <br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 删除文件夹<br>      *<br>      * @access     public<br>      * @param     string     目录标识(ftp)<br>      * @return    boolean<br>      */<br>     public function delete_dir($path) {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         //对目录宏的'/'字符添加反斜杠'\'<br>         $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);<br>     <br>         //获取目录文件列表<br>         $filelist = $this->filelist($path);<br>         <br>         if($filelist !== FALSE AND count($filelist) > 0) {<br>             foreach($filelist as $item) {<br>                 //如果我们无法删除,那么就可能是一个文件夹<br>                 //所以我们递归调用delete_dir()<br>                 if( ! @delete_file($item)) {<br>                     $this->delete_dir($item);<br>                 }<br>             }<br>         }<br>         <br>         //删除文件夹(空文件夹)<br>         $result = @ftp_rmdir($this->conn_id, $path);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");<br>             }<br>             return FALSE;<br>         }<br>         <br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 修改文件权限<br>      *<br>      * @access     public<br>      * @param     string     目录标识(ftp)<br>      * @return    boolean<br>      */<br>     public function chmod($path, $perm) {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         //只有在PHP5中才定义了修改权限的函数(ftp)<br>         if( ! function_exists('ftp_chmod')) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_chmod(function)");<br>             }<br>             return FALSE;<br>         }<br>         <br>         $result = @ftp_chmod($this->conn_id, $perm, $path);<br>         <br>         if($result === FALSE) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");<br>             }<br>             return FALSE;<br>         }<br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 获取目录文件列表<br>      *<br>      * @access     public<br>      * @param     string     目录标识(ftp)<br>      * @return    array<br>      */<br>     public function filelist($path = '.') {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         return ftp_nlist($this->conn_id, $path);<br>     }<br>     <br>     /**<br>      * 关闭FTP<br>      *<br>      * @access     public<br>      * @return    boolean<br>      */<br>     public function close() {<br>         if( ! $this->_isconn()) {<br>             return FALSE;<br>         }<br>         <br>         return @ftp_close($this->conn_id);<br>     }<br>     <br>     /**<br>      * FTP成员变量初始化<br>      *<br>      * @access    private<br>      * @param    array    配置数组     <br>      * @return    void<br>      */<br>     private function _init($config = array()) {<br>         foreach($config as $key => $val) {<br>             if(isset($this->$key)) {<br>                 $this->$key = $val;<br>             }<br>         }<br> <br>         //特殊字符过滤<br>         $this->hostname = preg_replace('|.+?://|','',$this->hostname);<br>     }<br>     <br>     /**<br>      * FTP登陆<br>      *<br>      * @access     private<br>      * @return    boolean<br>      */<br>     private function _login() {<br>         return @ftp_login($this->conn_id, $this->username, $this->password);<br>     }<br>     <br>     /**<br>      * 判断con_id<br>      *<br>      * @access     private<br>      * @return    boolean<br>      */<br>     private function _isconn() {<br>         if( ! is_resource($this->conn_id)) {<br>             if($this->debug === TRUE) {<br>                 $this->_error("ftp_no_connection");<br>             }<br>             return FALSE;<br>         }<br>         return TRUE;<br>     }<br>     <br>     /**<br>      * 从文件名中获取后缀扩展<br>      *<br>      * @access     private<br>      * @param     string     目录标识<br>      * @return    string<br>      */<br>     private function _getext($filename) {<br>         if(FALSE === strpos($filename, '.')) {<br>             return 'txt';<br>         }<br>         <br>         $extarr = explode('.', $filename);<br>         return end($extarr);<br>     }<br>     <br>     /**<br>      * 从后缀扩展定义FTP传输模式  ascii 或 binary<br>      *<br>      * @access     private<br>      * @param     string     后缀扩展<br>      * @return    string<br>      */<br>     private function _settype($ext) {<br>         $text_type = array (<br>                             'txt',<br>                             'text',<br>                             'php',<br>                             'phps',<br>                             'php4',<br>                             'js',<br>                             'css',<br>                             'htm',<br>                             'html',<br>                             'phtml',<br>                             'shtml',<br>                             'log',<br>                             'xml'<br>                             );<br>         <br>         return (in_array($ext, $text_type)) ? 'ascii' : 'binary';<br>     }<br>     <br>     /**<br>      * 错误日志记录<br>      *<br>      * @access     prvate<br>      * @return    boolean<br>      */<br>     private function _error($msg) {<br>         return @file_put_contents('ftp_err.log', "date[".date("Y-m-d H:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]\n", FILE_APPEND);<br>     }<br> }<br> <br> <br> <br> $config = array(<br>             'hostname' => '124.160.116.76',<br>             'username' => 'hangye_lsp_19@192.168.18.19',<br>             'password' => 'tb6bCnHNrO',<br>             'port' => 52816<br>                 );<br> <br> $ftp = new Ftp();<br> <br> $ftp->connect($config);<br> $ftp->upload('localfile.log','remotefile.log');<br> ?>

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn