工作日志 - FTP上传类源代码

本文介绍了一个PHP实现的FTP类,该类用于处理FTP文件上传功能,包括上传单个和多个文件、统计已上传文件数量及大小、关闭FTP目录等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php

/**********************************************************
* palaa.org  WEB 平台
*
* @classfilename FTP.class.php 
* @classname FTP
* @classpath @mod/ftp/
* @memo      FTP类 ,用于FTP文件上传
***********************************************************/

class FTP {


   //环境变量
   protected $webdir; // 基础目录;
   protected $usefiles;   // 使用的状态;
   protected $usesize;   // 使用控件; 
   protected $quotafiles; //文件数
   protected $quotasize; //文件大小
   protected $filesize; //上传文件大小 
   protected   $ifquitftp;   //是否关闭ftp目录
   protected   $stattotal;   //统计ftp目录下文件总数量;
   protected   $statsize;    //统计ftp目录下文件总大小   
   function __construct($useinfo){
    $this->quotafiles = $useinfo['quotafiles'];
    $this->quotasize  = $useinfo['quotasize'];
    $this->webdir   = $useinfo['webdir'];
    $this->readuse();
   }
   private function __set($key,$value){
     $this->{$key} = $value;
     if ($key=='webdir'){
      $this->readuse();
     }
   }
   private function readuse(){         //读出已上传文件配额信息
     $use = File::read($this->webdir.'/info.txt');
     list($files,$size ) = explode(" ", $use);
     $this->usefiles = (int)$files;
     $this->usesize = (int)$size;   
   }
   private function ifover($addfiles,$addsize){           //判断是否超过配额
     if ( $this->usefiles+$addfiles > $this->quotafiles){
      return true;
     }
     if ( $this->usesize+$addsize > $this->quotasize){
      return true;
     }
     return false;
   }
   private function writeuse($addfiles,$addsize){     //写入已使用的配额
      $this->usesize  += $addsize;
      $this->usefiles += $addfiles;
      $data     = sprintf('%s %s',$this->usefiles,$this->usesize);      
      File::write($this->webdir.'/info.txt',$data); 
   }   
   public function upload($objpath,$ctrlname=''){
     $result = false;
     $objpath .= '/';
     if ($ctrlname!=''&& ($files=$_FILES[$ctrlname])){
        if ( is_array($files['name']) ){
         $result = $this->saves($files,$objpath);
        }else{
         $result =$this->save($files,$objpath);
        }
        return $result;
     }
     foreach($_FILES as $files){
        if ( is_array($files['name']) ){
         $result = $this->saves($files,$objpath);
        }else{
         $result = $this->save($files,$objpath);
        }
        if (!$result){
         return $result;
        }
     }
     return  $result;
   }
   protected function save($afileinfo,$objpath,$filename=''){
      $filename == '' && $filename = $afileinfo['name'];
        if ($afileinfo['error']==0){
        if (is_uploaded_file($afileinfo['tmp_name'])){
        $filesize = (int)$afileinfo['size'];   //上传的文件大小
        if ( $this->ifover(1,$filesize) ){
         return false;
        }      
        is_dir($objpath)||Dir::mk($objpath);
        @move_uploaded_file($afileinfo['tmp_name'],$objpath."/$filename");
        $this->writeuse(1,$filesize);
           //$this->closeftp();  //将ftp目录关闭
        //$this->delftpdir(); //删除ftp目录
        return true;
        }
        }
        return false;
   } 
   protected function saves($files,$objpath,$filename=''){
      $result = false;
      foreach($files['name'] as $key => $name){
      $afileinfo['name']  = $files['name'][$key];
      $afileinfo['type']   = $files['type'][$key];
      $afileinfo['tmp_name']  = $files['tmp_name'][$key];
      $afileinfo['error']  = $files['error'][$key];
      $afileinfo['size']  = $files['size'][$key];                               
      $result = $this->save($afileinfo,$objpath);
      if (!$result){return false;}
      }
      return false;
   }
   
   public function statftp(){                                                             //统计已上传FTP文件信息
     if(!file_exists("./$this->webdir/.")){  //若目录存在,则获取目录大小
       Dir::files($this->webdir);
       $this->stattotal = count($files);  //FTP文件总大小
       foreach($files as $value){  
        $files['size'] += $value;
       }
       $this->statsize = $files['size'];
       //空间大小单位转换     
       File::formatsize($statsize);
     }else{              //目录不存在,则返回0值
       $this->stattotal = 0 && $this->statsize = 0;
     } 
   }
   
   public function closeftp(){                                                               //设置此目录为关闭目录
     if(file_exists("./$this->webdir/.")){
      rename($this->webdir,$this->webdir.'.close') && $this->ifquitftp = 1;  
     }
     if(file_exists("./$this->webdir.'.close'/.")){
      $this->ifquitftp = 1;  
     }
     return false;
   }
   
   public function delftpdir(){  //FTP目录删除
     $this->ifquitftp==1 && Dir::rm($this->webdir.'.close');
   }
   
   public function html($uploadpath='/tmp/',$urlpath='/'){
    if ($urlpath == '/'){
     $url =  parse_url($_SERVER['REQUEST_URI']);   
     $url = "http://{$_SERVER['HTTP_HOST']}".dirname($url['path']).'/';
    }
    $flashfile = MOD_PATH.'ftp/ftp.swf?sn=path&sid='."$uploadpath&url=$url&t=P";  
    return '<div class="flash_input_file"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="470" height="350">
     <param name="movie" value="'.$flashfile.'">
     <param name="quality" value="high">
     <embed src="'.$flashfile.'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="470" height="350"></embed>
    </object></div>';     
   }
   public function lists($path='/'){
     $path = $this->webdir.str_replace('..','.',$path);
     $files = Dir::file($path);
     $dirs = Dir::dir($path); 
     $result = '<input type="hidden" name="curobjname">';
     
     
     $result .= '<table><tr>';
     $xi = 0;
       foreach($dirs as $dir){
         $xi++;
         $result .= "<td align=center>".$this->showfolder($dir)."</td>";
         if($xi%7 == 0)
         {
          $result .="</tr><tr>";
         }
       }
       foreach($files as $file){
         $xi++;
         $result .= "<td align=center>".$this->showfile($file)."</td>";
         if($xi%7 ==0){
          $result .="</tr><tr>";
         }
         
       }
       $xi++;
     $result .="</tr></table>";
     
      
     $xform =  new Xform(''); 
     $xform->innerHTML = $this->splitfileurl($path).$result;
     return  ''.$xform;
   }
   public function showfolder($name){
     $img = 'res/image/icons/f_folder.gif';
     $formatstr = '<br><span><label ōnclick="form.curobjname.value=/'%s/';" ōndblclick="form.submit();" ><img src="%s" /><br>%s</label></span>';
     return sprintf($formatstr,$name,$img,File::noext($name));   
   }
   public function showfile($name){
     $exts  = array('gif','jpg','txt');
     $ext = strtolower(File::ext($name));
     $ext = in_array($ext,$exts) ? $ext:'unknown';
     $img = 'res/image/icons/f_file_'.$ext.".gif";     
     $strfilename = File::name($name);
     list($strname,$tag) = explode(".",$strfilename);
     if(strlen($strname)>8){
      $newname = substr($strname,0,8)."...".$tag;
     }else{
      $newname = $strname.".".$tag;
     }
     $formatstr = '<br><span title='.$strfilename.'><label  ōnclick="form.curobjname.value=/'%s/';" ōndblclick="form.submit();" ><img src="%s" /><br>%s</label></a></span>';
     return sprintf($formatstr,$name,$img,$newname);   
   }
   public function splitfileurl($path){
     $paths = explode('/',str_replace('//','/',$path));
     $url =  $GLOBALS['url'].'&amp;webdir=';
     foreach($paths as $path){
      $formatstr = ' &gt;<a href="%s">%s</a>';
      $url = "$url/$path";
      $result .= sprintf($formatstr,$url,$path);
     }
     return "<div class=/"pwd/">$result</div><br";
   }
}


?>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值