分享

PHP的FTP类

 汉江秋月夜 2012-08-03
邪恶的小Y

PHP的FTP类

邪恶的小Y 发布于 2011年08月18日 14时, 6评/2182阅 12人收藏此代码, 我要收藏
0
仿写CodeIgniter的FTP类
  FTP基本操作:
 1) 登陆                     connect
 2) 当前目录文件列表    filelist
 3) 目录改变               chgdir
 4) 重命名/移动           rename
 5) 创建文件夹            mkdir
 6) 删除                     delete_dir/delete_file
 7) 上传                     upload
 8) 下载                     download
标签: CodeIgniter

代码片段(2)

[文件] ftp.php ~ 9KB    下载(332)

001?<?php
002/**
003 * 仿写CodeIgniter的FTP类
004 * FTP基本操作:
005 * 1) 登陆;           connect
006 * 2) 当前目录文件列表;  filelist
007 * 3) 目录改变;         chgdir
008 * 4) 重命名/移动;       rename
009 * 5) 创建文件夹;        mkdir
010 * 6) 删除;               delete_dir/delete_file
011 * 7) 上传;               upload
012 * 8) 下载                download
013 *
014 * @author quanshuidingdang
015 */
016class Ftp {
017 
018    private $hostname   = '';
019    private $username   = '';
020    private $password   = '';
021    private $port       = 21;
022    private $passive    = TRUE;
023    private $debug      = TRUE;
024    private $conn_id    = FALSE;
025     
026    /**
027     * 构造函数
028     *
029     * @param   array   配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
030     */
031    public function __construct($config = array()) {
032        if(count($config) > 0) {
033            $this->_init($config);
034        }
035    }
036     
037    /**
038     * FTP连接
039     *
040     * @access  public
041     * @param   array   配置数组
042     * @return  boolean
043     */
044    public function connect($config = array()) {
045        if(count($config) > 0) {
046            $this->_init($config);
047        }
048         
049        if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
050            if($this->debug === TRUE) {
051                $this->_error("ftp_unable_to_connect");
052            }
053            return FALSE;
054        }
055         
056        if( ! $this->_login()) {
057            if($this->debug === TRUE) {
058                $this->_error("ftp_unable_to_login");
059            }
060            return FALSE;
061        }
062         
063        if($this->passive === TRUE) {
064            ftp_pasv($this->conn_id, TRUE);
065        }
066         
067        return TRUE;
068    }
069 
070     
071    /**
072     * 目录改变
073     *
074     * @access  public
075     * @param   string  目录标识(ftp)
076     * @param   boolean
077     * @return  boolean
078     */
079    public function chgdir($path = '', $supress_debug = FALSE) {
080        if($path == '' OR ! $this->_isconn()) {
081            return FALSE;
082        }
083         
084        $result = @ftp_chdir($this->conn_id, $path);
085         
086        if($result === FALSE) {
087            if($this->debug === TRUE AND $supress_debug == FALSE) {
088                $this->_error("ftp_unable_to_chgdir:dir[".$path."]");
089            }
090            return FALSE;
091        }
092         
093        return TRUE;
094    }
095     
096    /**
097     * 目录生成
098     *
099     * @access  public
100     * @param   string  目录标识(ftp)
101     * @param   int     文件权限列表 
102     * @return  boolean
103     */
104    public function mkdir($path = '', $permissions = NULL) {
105        if($path == '' OR ! $this->_isconn()) {
106            return FALSE;
107        }
108         
109        $result = @ftp_mkdir($this->conn_id, $path);
110         
111        if($result === FALSE) {
112            if($this->debug === TRUE) {
113                $this->_error("ftp_unable_to_mkdir:dir[".$path."]");
114            }
115            return FALSE;
116        }
117         
118        if( ! is_null($permissions)) {
119            $this->chmod($path,(int)$permissions);
120        }
121         
122        return TRUE;
123    }
124     
125    /**
126     * 上传
127     *
128     * @access  public
129     * @param   string  本地目录标识
130     * @param   string  远程目录标识(ftp)
131     * @param   string  上传模式 auto || ascii
132     * @param   int     上传后的文件权限列表 
133     * @return  boolean
134     */
135    public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
136        if( ! $this->_isconn()) {
137            return FALSE;
138        }
139         
140        if( ! file_exists($localpath)) {
141            if($this->debug === TRUE) {
142                $this->_error("ftp_no_source_file:".$localpath);
143            }
144            return FALSE;
145        }
146         
147        if($mode == 'auto') {
148            $ext = $this->_getext($localpath);
149            $mode = $this->_settype($ext);
150        }
151         
152        $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
153         
154        $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
155         
156        if($result === FALSE) {
157            if($this->debug === TRUE) {
158                $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
159            }
160            return FALSE;
161        }
162         
163        if( ! is_null($permissions)) {
164            $this->chmod($remotepath,(int)$permissions);
165        }
166         
167        return TRUE;
168    }
169     
170    /**
171     * 下载
172     *
173     * @access  public
174     * @param   string  远程目录标识(ftp)
175     * @param   string  本地目录标识
176     * @param   string  下载模式 auto || ascii 
177     * @return  boolean
178     */
179    public function download($remotepath, $localpath, $mode = 'auto') {
180        if( ! $this->_isconn()) {
181            return FALSE;
182        }
183         
184        if($mode == 'auto') {
185            $ext = $this->_getext($remotepath);
186            $mode = $this->_settype($ext);
187        }
188         
189        $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
190         
191        $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
192         
193        if($result === FALSE) {
194            if($this->debug === TRUE) {
195                $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
196            }
197            return FALSE;
198        }
199         
200        return TRUE;
201    }
202     
203    /**
204     * 重命名/移动
205     *
206     * @access  public
207     * @param   string  远程目录标识(ftp)
208     * @param   string  新目录标识
209     * @param   boolean 判断是重命名(FALSE)还是移动(TRUE)
210     * @return  boolean
211     */
212    public function rename($oldname, $newname, $move = FALSE) {
213        if( ! $this->_isconn()) {
214            return FALSE;
215        }
216         
217        $result = @ftp_rename($this->conn_id, $oldname, $newname);
218         
219        if($result === FALSE) {
220            if($this->debug === TRUE) {
221                $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
222                $this->_error($msg);
223            }
224            return FALSE;
225        }
226         
227        return TRUE;
228    }
229     
230    /**
231     * 删除文件
232     *
233     * @access  public
234     * @param   string  文件标识(ftp)
235     * @return  boolean
236     */
237    public function delete_file($file) {
238        if( ! $this->_isconn()) {
239            return FALSE;
240        }
241         
242        $result = @ftp_delete($this->conn_id, $file);
243         
244        if($result === FALSE) {
245            if($this->debug === TRUE) {
246                $this->_error("ftp_unable_to_delete_file:file[".$file."]");
247            }
248            return FALSE;
249        }
250         
251        return TRUE;
252    }
253     
254    /**
255     * 删除文件夹
256     *
257     * @access  public
258     * @param   string  目录标识(ftp)
259     * @return  boolean
260     */
261    public function delete_dir($path) {
262        if( ! $this->_isconn()) {
263            return FALSE;
264        }
265         
266        //对目录宏的'/'字符添加反斜杠'\'
267        $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
268     
269        //获取目录文件列表
270        $filelist = $this->filelist($path);
271         
272        if($filelist !== FALSE AND count($filelist) > 0) {
273            foreach($filelist as $item) {
274                //如果我们无法删除,那么就可能是一个文件夹
275                //所以我们递归调用delete_dir()
276                if( ! @delete_file($item)) {
277                    $this->delete_dir($item);
278                }
279            }
280        }
281         
282        //删除文件夹(空文件夹)
283        $result = @ftp_rmdir($this->conn_id, $path);
284         
285        if($result === FALSE) {
286            if($this->debug === TRUE) {
287                $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
288            }
289            return FALSE;
290        }
291         
292        return TRUE;
293    }
294     
295    /**
296     * 修改文件权限
297     *
298     * @access  public
299     * @param   string  目录标识(ftp)
300     * @return  boolean
301     */
302    public function chmod($path, $perm) {
303        if( ! $this->_isconn()) {
304            return FALSE;
305        }
306         
307        //只有在PHP5中才定义了修改权限的函数(ftp)
308        if( ! function_exists('ftp_chmod')) {
309            if($this->debug === TRUE) {
310                $this->_error("ftp_unable_to_chmod(function)");
311            }
312            return FALSE;
313        }
314         
315        $result = @ftp_chmod($this->conn_id, $perm, $path);
316         
317        if($result === FALSE) {
318            if($this->debug === TRUE) {
319                $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
320            }
321            return FALSE;
322        }
323        return TRUE;
324    }
325     
326    /**
327     * 获取目录文件列表
328     *
329     * @access  public
330     * @param   string  目录标识(ftp)
331     * @return  array
332     */
333    public function filelist($path = '.') {
334        if( ! $this->_isconn()) {
335            return FALSE;
336        }
337         
338        return ftp_nlist($this->conn_id, $path);
339    }
340     
341    /**
342     * 关闭FTP
343     *
344     * @access  public
345     * @return  boolean
346     */
347    public function close() {
348        if( ! $this->_isconn()) {
349            return FALSE;
350        }
351         
352        return @ftp_close($this->conn_id);
353    }
354     
355    /**
356     * FTP成员变量初始化
357     *
358     * @access  private
359     * @param   array   配置数组    
360     * @return  void
361     */
362    private function _init($config = array()) {
363        foreach($config as $key => $val) {
364            if(isset($this->$key)) {
365                $this->$key = $val;
366            }
367        }
368 
369        //特殊字符过滤
370        $this->hostname = preg_replace('|.+?://|','',$this->hostname);
371    }
372     
373    /**
374     * FTP登陆
375     *
376     * @access  private
377     * @return  boolean
378     */
379    private function _login() {
380        return @ftp_login($this->conn_id, $this->username, $this->password);
381    }
382     
383    /**
384     * 判断con_id
385     *
386     * @access  private
387     * @return  boolean
388     */
389    private function _isconn() {
390        if( ! is_resource($this->conn_id)) {
391            if($this->debug === TRUE) {
392                $this->_error("ftp_no_connection");
393            }
394            return FALSE;
395        }
396        return TRUE;
397    }
398     
399    /**
400     * 从文件名中获取后缀扩展
401     *
402     * @access  private
403     * @param   string  目录标识
404     * @return  string
405     */
406    private function _getext($filename) {
407        if(FALSE === strpos($filename, '.')) {
408            return 'txt';
409        }
410         
411        $extarr = explode('.', $filename);
412        return end($extarr);
413    }
414     
415    /**
416     * 从后缀扩展定义FTP传输模式  ascii 或 binary
417     *
418     * @access  private
419     * @param   string  后缀扩展
420     * @return  string
421     */
422    private function _settype($ext) {
423        $text_type = array (
424                            'txt',
425                            'text',
426                            'php',
427                            'phps',
428                            'php4',
429                            'js',
430                            'css',
431                            'htm',
432                            'html',
433                            'phtml',
434                            'shtml',
435                            'log',
436                            'xml'
437                            );
438         
439        return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
440    }
441     
442    /**
443     * 错误日志记录
444     *
445     * @access  prvate
446     * @return  boolean
447     */
448    private function _error($msg) {
449        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);
450    }
451}
452 
453/*End of file ftp.php*/
454/*Location /Apache Group/htdocs/ftp.php*/

[文件] ftp_demo.php ~ 376B    下载(197)

01<?php
02require_once('ftp.php');
03 
04$config = array(
05            'hostname' => 'localhost',
06            'username' => 'root',
07            'password' => 'root',
08            'port' => 21
09                );
10 
11$ftp = new Ftp();
12 
13$ftp->connect($config);
14$ftp->upload('ftp_err.log','ftp_upload.log');
15$ftp->download('ftp_upload.log','ftp_download.log');
16 
17/*End of file ftp_demo.php*/
18/*Location: /htdocs/ftp_demo.php*/

开源中国-程序员在线工具:API文档大全(120+) JS在线编辑演示 二维码 更多>>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多