123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- //帝国网站管理系统FTP
- define('InEmpireCMSFtp',TRUE);
- class EmpireCMSFTP{
- var $ftpconnectid;
- var $ftptranmode;
- function wipespecial($str){
- return str_replace(array("\n","\r"),array('',''),$str);
- }
- //链接
- function fconnect($ftphost,$ftpport,$ftpusername,$ftppassword,$ftppath,$ftpssl=0,$pasv=0,$tranmode=0,$timeout=0,$checkftp=0){
- $ftphost=$this->wipespecial($ftphost);
- $func=$ftpssl&&function_exists('ftp_ssl_connect')?'ftp_ssl_connect':'ftp_connect';
- $this->ftpconnectid=@$func($ftphost,$ftpport,20);
- if(!$this->ftpconnectid)
- {
- if($checkftp==1)
- {
- return 'HostFail';
- }
- echo"Fail to connect ftp host!";
- exit();
- }
- if($timeout&&function_exists('ftp_set_option'))
- {
- @ftp_set_option($this->ftpconnectid,FTP_TIMEOUT_SEC,$timeout);
- }
- $login=$this->fLogin($ftpusername,$ftppassword);
- if(!$login)
- {
- if($checkftp==1)
- {
- $this->fExit();
- return 'UserFail';
- }
- echo"The username/password for ftp is error!";
- $this->fExit();
- exit();
- }
- if($pasv)
- {
- $this->fPasv(TRUE);
- }
- $ftppath=empty($ftppath)?'/':$ftppath;
- $chdir=$this->fChdir($ftppath);
- if(!$chdir)
- {
- if($checkftp==1)
- {
- $this->fExit();
- return 'PathFail';
- }
- echo"The path for ftp is error!";
- $this->fExit();
- exit();
- }
- $this->ftptranmode=$tranmode?FTP_ASCII:FTP_BINARY;
- }
- //登录
- function fLogin($username,$password) {
- $username=$this->wipespecial($username);
- $password=$this->wipespecial($password);
- return @ftp_login($this->ftpconnectid,$username,$password);
- }
- //关闭ftp
- function fExit(){
- return @ftp_quit($this->ftpconnectid);
- }
- //链接模式
- function fPasv($pasv){
- return @ftp_pasv($this->ftpconnectid,$pasv);
- }
- //改变路径
- function fChdir($path){
- $path=$this->wipespecial($path);
- return @ftp_chdir($this->ftpconnectid,$path);
- }
-
- //建立目录
- function fMkdir($path){
- $path=$this->wipespecial($path);
- return @ftp_mkdir($this->ftpconnectid,$path);
- }
- //向服务器发送 SITE 命令
- function fSiteCmd($cmd){
- $cmd=$this->wipespecial($cmd);
- return @ftp_site($this->ftpconnectid,$cmd);
- }
- //设置目录权限
- function fChmoddir($mode,$filename){
- $mode=intval($mode);
- $filename=$this->wipespecial($filename);
- if(function_exists('ftp_chmod'))
- {
- return @ftp_chmod($this->ftpconnectid,$mode,$filename);
- }
- else
- {
- return $this->fSiteCmd('CHMOD '.$mode.' '.$filename);
- }
- }
- //删除目录
- function fRmdir($path){
- $path=$this->wipespecial($path);
- return @ftp_rmdir($this->ftpconnectid,$path);
- }
- //上传文件
- function fTranFile($hfile,$lfile,$startpos=0,$del=0){
- $hfile=$this->wipespecial($hfile);
- $lfile=$this->wipespecial($lfile);
- $startpos=intval($startpos);
- $tran=@ftp_put($this->ftpconnectid,$hfile,$lfile,$this->ftptranmode,$startpos);
- if($del)
- {
- DelFiletext($lfile);
- }
- return $tran;
- }
- //上传单文件(含建目录)
- function fTranPathFile($basepath,$path,$hfile,$lfile,$del=0){
- //建目录
- $this->ftp_mkdirs($basepath,$path);
- //上传文件
- $this->fTranFile($hfile,$lfile,0,$del);
- }
- //上传多文件
- function fMoreTranFile($hfile,$lfile,$del=0){
- $count=count($hfile);
- for($i=0;$i<$count;$i++)
- {
- $this->fTranFile($hfile[$i],$lfile[$i],0,$del);
- }
- }
- //上传多文件(含建目录)
- function fMoreTranPathFile($basepath,$path,$hfile,$lfile,$del=0){
- //建目录
- $this->ftp_mkdirs($basepath,$path);
- //上传文件
- $this->fMoreTranFile($hfile,$lfile,$del);
- }
- //下载文件
- function fGetFile($lfile,$hfile,$resumepos=0){
- $hfile=$this->wipespecial($hfile);
- $lfile=$this->wipespecial($lfile);
- $resumepos=intval($resumepos);
- return @ftp_get($this->ftpconnectid,$lfile,$hfile,$this->ftptranmode,$resumepos);
- }
- //文件大小
- function fSize($hfile){
- $hfile=$this->wipespecial($hfile);
- return @ftp_size($this->ftpconnectid,$hfile);
- }
- //删除文件
- function fDelFile($hfile){
- $hfile=$this->wipespecial($hfile);
- return @ftp_delete($this->ftpconnectid,$hfile);
- }
- //删除多文件
- function fMoreDelFile($hfile){
- $count=count($hfile);
- for($i=0;$i<$count;$i++)
- {
- $this->fDelFile($hfile[$i]);
- }
- }
- //重命名文件
- function fRename($oldfile,$newfile){
- $oldfile=$this->wipespecial($oldfile);
- $newfile=$this->wipespecial($newfile);
- return @ftp_rename($this->ftpconnectid,$oldfile,$newfile);
- }
- //获得当前路径
- function fPwd(){
- return @ftp_pwd($this->ftpconnectid);
- }
- //上传目录
- function ftp_copy($src_dir,$dst_dir){
- $src_dir=$this->wipespecial($src_dir);
- $dst_dir=$this->wipespecial($dst_dir);
- if(!$this->fChdir($dst_dir))
- {
- $this->fMkdir($dst_dir);
- }
- $d=@opendir($src_dir);
- while($file=@readdir($d))
- {
- if($file!= "."&&$file!="..")
- {
- if(is_dir($src_dir."/".$file))
- {
- $this->ftp_copy($src_dir."/".$file,$dst_dir."/".$file);
- }
- else
- {
- $this->fTranFile($dst_dir."/".$file,$src_dir."/".$file);
- }
- }
- }
- @closedir($d);
- }
- //返回目录的文件列表
- function fNlist($path) {
- $path=$this->wipespecial($path);
- return @ftp_nlist($this->ftpconnectid,$path);
- }
- //删除目录
- function ftp_rmAll($path,$flag=true){
- $path=$this->wipespecial($path);
- if($flag)
- {
- $ret=$this->fRmdir($path)||$this->fDelFile($path);
- }
- else
- {
- $ret=false;
- }
- if(!$ret)
- {
- $files=$this->fNlist($path);
- foreach($files as $values)
- {
- $values=basename($values);
- $dirfile=$path.'/'.$values;
- if($this->fSize($dirfile)==-1)
- {
- $this->fDelFile($dirfile);
- }
- else
- {
- $this->ftp_rmAll($dirfile,true);
- }
- }
- if($flag)
- {
- return $this->fRmdir($path);
- }
- else
- {
- return true;
- }
- }
- else
- {
- return $ret;
- }
- }
- //建多目录
- function ftp_mkdirs($basepath,$path){
- $basepath=$this->wipespecial($basepath);
- $path=$this->wipespecial($path);
- if(empty($path))
- {
- return '';
- }
- $r=explode('/',$path);
- $count=count($r);
- for($i=0;$i<$count;$i++)
- {
- if($i>0)
- {
- $returnpath.='/'.$r[$i];
- }
- else
- {
- $returnpath.=$r[$i];
- }
- $createpath=$basepath.$returnpath;
- if(!$this->fChdir($createpath))
- {
- $mk=$this->fMkdir($createpath);
- if(empty($mk))
- {
- printerror("CreatePathFail","");
- }
- }
- }
- }
- }
- ?>
|