仿寫CodeIgniter的FTP類
FTP基本操作:
1) 登陸 connect
2) 當前目錄文件列表 filelist
3) 目錄改變 chgdir
4) 重命名/移動 rename
5) 創(chuàng)建文件夾 mkdir
6) 刪除 delete_dir/delete_file
7) 上傳 upload
8) 下載 download
[文件] ftp.php ~ 9KB 下載(332)
018 | private $hostname = '' ; |
019 | private $username = '' ; |
020 | private $password = '' ; |
022 | private $passive = TRUE; |
023 | private $debug = TRUE; |
024 | private $conn_id = FALSE; |
031 | public function __construct( $config = array ()) { |
032 | if ( count ( $config ) > 0) { |
033 | $this ->_init( $config ); |
044 | public function connect( $config = array ()) { |
045 | if ( count ( $config ) > 0) { |
046 | $this ->_init( $config ); |
049 | if (FALSE === ( $this ->conn_id = @ftp_connect( $this ->hostname, $this ->port))) { |
050 | if ( $this ->debug === TRUE) { |
051 | $this ->_error( "ftp_unable_to_connect" ); |
056 | if ( ! $this ->_login()) { |
057 | if ( $this ->debug === TRUE) { |
058 | $this ->_error( "ftp_unable_to_login" ); |
063 | if ( $this ->passive === TRUE) { |
064 | ftp_pasv( $this ->conn_id, TRUE); |
079 | public function chgdir( $path = '' , $supress_debug = FALSE) { |
080 | if ( $path == '' OR ! $this ->_isconn()) { |
084 | $result = @ftp_chdir( $this ->conn_id, $path ); |
086 | if ( $result === FALSE) { |
087 | if ( $this ->debug === TRUE AND $supress_debug == FALSE) { |
088 | $this ->_error( "ftp_unable_to_chgdir:dir[" . $path . "]" ); |
104 | public function mkdir ( $path = '' , $permissions = NULL) { |
105 | if ( $path == '' OR ! $this ->_isconn()) { |
109 | $result = @ftp_mkdir( $this ->conn_id, $path ); |
111 | if ( $result === FALSE) { |
112 | if ( $this ->debug === TRUE) { |
113 | $this ->_error( "ftp_unable_to_mkdir:dir[" . $path . "]" ); |
118 | if ( ! is_null ( $permissions )) { |
119 | $this -> chmod ( $path ,(int) $permissions ); |
135 | public function upload( $localpath , $remotepath , $mode = 'auto' , $permissions = NULL) { |
136 | if ( ! $this ->_isconn()) { |
140 | if ( ! file_exists ( $localpath )) { |
141 | if ( $this ->debug === TRUE) { |
142 | $this ->_error( "ftp_no_source_file:" . $localpath ); |
147 | if ( $mode == 'auto' ) { |
148 | $ext = $this ->_getext( $localpath ); |
149 | $mode = $this ->_settype( $ext ); |
152 | $mode = ( $mode == 'ascii' ) ? FTP_ASCII : FTP_BINARY; |
154 | $result = @ftp_put( $this ->conn_id, $remotepath , $localpath , $mode ); |
156 | if ( $result === FALSE) { |
157 | if ( $this ->debug === TRUE) { |
158 | $this ->_error( "ftp_unable_to_upload:localpath[" . $localpath . "]/remotepath[" . $remotepath . "]" ); |
163 | if ( ! is_null ( $permissions )) { |
164 | $this -> chmod ( $remotepath ,(int) $permissions ); |
179 | public function download( $remotepath , $localpath , $mode = 'auto' ) { |
180 | if ( ! $this ->_isconn()) { |
184 | if ( $mode == 'auto' ) { |
185 | $ext = $this ->_getext( $remotepath ); |
186 | $mode = $this ->_settype( $ext ); |
189 | $mode = ( $mode == 'ascii' ) ? FTP_ASCII : FTP_BINARY; |
191 | $result = @ftp_get( $this ->conn_id, $localpath , $remotepath , $mode ); |
193 | if ( $result === FALSE) { |
194 | if ( $this ->debug === TRUE) { |
195 | $this ->_error( "ftp_unable_to_download:localpath[" . $localpath . "]-remotepath[" . $remotepath . "]" ); |
212 | public function rename( $oldname , $newname , $move = FALSE) { |
213 | if ( ! $this ->_isconn()) { |
217 | $result = @ftp_rename( $this ->conn_id, $oldname , $newname ); |
219 | if ( $result === FALSE) { |
220 | if ( $this ->debug === TRUE) { |
221 | $msg = ( $move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move" ; |
237 | public function delete_file( $file ) { |
238 | if ( ! $this ->_isconn()) { |
242 | $result = @ftp_delete( $this ->conn_id, $file ); |
244 | if ( $result === FALSE) { |
245 | if ( $this ->debug === TRUE) { |
246 | $this ->_error( "ftp_unable_to_delete_file:file[" . $file . "]" ); |
261 | public function delete_dir( $path ) { |
262 | if ( ! $this ->_isconn()) { |
267 | $path = preg_replace( "/(.+?)\/*$/" , "\\1/" , $path ); |
270 | $filelist = $this ->filelist( $path ); |
272 | if ( $filelist !== FALSE AND count ( $filelist ) > 0) { |
273 | foreach ( $filelist as $item ) { |
276 | if ( ! @delete_file( $item )) { |
277 | $this ->delete_dir( $item ); |
283 | $result = @ftp_rmdir( $this ->conn_id, $path ); |
285 | if ( $result === FALSE) { |
286 | if ( $this ->debug === TRUE) { |
287 | $this ->_error( "ftp_unable_to_delete_dir:dir[" . $path . "]" ); |
299 | * @param string 目錄標識(ftp) |
302 | public function chmod ( $path , $perm ) { |
303 | if ( ! $this ->_isconn()) { |
308 | if ( ! function_exists( 'ftp_chmod' )) { |
309 | if ( $this ->debug === TRUE) { |
310 | $this ->_error( "ftp_unable_to_chmod(function)" ); |
315 | $result = @ftp_chmod( $this ->conn_id, $perm , $path ); |
317 | if ( $result === FALSE) { |
318 | if ( $this ->debug === TRUE) { |
319 | $this ->_error( "ftp_unable_to_chmod:path[" . $path . "]-chmod[" . $perm . "]" ); |
333 | public function filelist( $path = '.' ) { |
334 | if ( ! $this ->_isconn()) { |
338 | return ftp_nlist( $this ->conn_id, $path ); |
347 | public function close() { |
348 | if ( ! $this ->_isconn()) { |
352 | return @ftp_close( $this ->conn_id); |
362 | private function _init( $config = array ()) { |
363 | foreach ( $config as $key => $val ) { |
364 | if (isset( $this -> $key )) { |
370 | $this ->hostname = preg_replace( '|.+?://|' , '' , $this ->hostname); |
379 | private function _login() { |
380 | return @ftp_login( $this ->conn_id, $this ->username, $this ->password); |
389 | private function _isconn() { |
390 | if ( ! is_resource ( $this ->conn_id)) { |
391 | if ( $this ->debug === TRUE) { |
392 | $this ->_error( "ftp_no_connection" ); |
406 | private function _getext( $filename ) { |
407 | if (FALSE === strpos ( $filename , '.' )) { |
411 | $extarr = explode ( '.' , $filename ); |
422 | private function _settype( $ext ) { |
439 | return (in_array( $ext , $text_type )) ? 'ascii' : 'binary' ; |
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); |
[文件] ftp_demo.php ~ 376B 下載(197)
02 | require_once ( 'ftp.php' ); |
05 | 'hostname' => 'localhost' , |
13 | $ftp ->connect( $config ); |
14 | $ftp ->upload( 'ftp_err.log' , 'ftp_upload.log' ); |
15 | $ftp ->download( 'ftp_upload.log' , 'ftp_download.log' ); |
開源中國-程序員在線工具:API文檔大全(120+) JS在線編輯演示 二維碼 更多>>
|