日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

PHP的FTP類

 漢江秋月夜 2012-08-03
邪惡的小Y

PHP的FTP類

邪惡的小Y 發(fā)布于 2011年08月18日 14時, 6評/2182閱 12人收藏此代碼, 我要收藏
0
仿寫CodeIgniter的FTP類
  FTP基本操作:
 1) 登陸                     connect
 2) 當前目錄文件列表    filelist
 3) 目錄改變               chgdir
 4) 重命名/移動           rename
 5) 創(chuàng)建文件夾            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) 創(chuàng)建文件夾;        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     * 構(gòu)造函數(shù)
028     *
029     * @param   array   配置數(shù)組 : $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   配置數(shù)組
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     文件權(quán)限列表 
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     上傳后的文件權(quán)限列表 
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                //所以我們遞歸調(diào)用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     * 修改文件權(quán)限
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中才定義了修改權(quán)限的函數(shù)(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     * 關(guān)閉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   配置數(shù)組    
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在線編輯演示 二維碼 更多>>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多