PHP中FTP模块的一些应用
作者 : admin 于 2008年08月27日, 10:20:06
2008
08-27
08-27
- <?php
- class ftp
- {
- //connect id
- var $ftp;
- //remote dir
- var $remotedir = '/';
- //local dir
- var $localdir = './';
- //write log
- var $writelog = false;
- //log file
- var $logfile = 'ftp_log.txt';
- //print log
- var $printlog = false;
- //session time
- var $timeout = 60;
- //construct connetc
- function __construct( $host , $user , $pass )
- {
- $this->ftp = @ftp_connect( $host );
- ftp_login( $this->ftp, $user , $pass );
- //ftp_set_option( $this->ftp , FTP_TIMEOUT_SEC , $this->timeout );
- }
- //list sub files
- function __list( $dir = false , $subtree = false )
- {
- $rlist = array();
- if(!$dir)
- {
- $dir = $this->remotedir;
- }
- $list = ftp_rawlist($this->ftp,$dir,TRUE);
- if(is_array($list))
- {
- foreach( $list as $key => $value )
- {
- $tmp = explode( ' ' , $value );
- $rlist[$key] = end($tmp);
- if(@ftp_chdir($this->ftp,$dir.$rlist[$key].'/'))
- {
- $rlist[$rlist[$key]] = $this->__list( $dir.$rlist[$key].'/' );
- unset($rlist[$key]);
- }
- unset($tmp);
- }
- }
- return $rlist;
- }
- //change dir $isabs = false,use rela dir default
- function __chdir( $dir , $isabs = false )
- {
- if(!$isabs)
- {
- $nextdir = $this->remotedir.$dir.'/';
- }
- else
- {
- $nextdir = $dir;
- }
- if(ftp_chdir($this->ftp,$nextdir))
- {
- $this->remotedir = $nextdir;
- return true;
- }
- else
- {
- return false;
- }
- }
- //close connect
- function __close()
- {
- @ftp_close($this->ftp);
- }
- }
- $ftp = new ftp( 'www.sunboyu.cn' , '**', '***' );
- $ftp->__chdir('website',0);
- $list = $ftp->__list('/');
- echo $a = time();
- print_r($list);
- echo time() - $a;
- ?>