一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php多文件上传代码实现php多文件上传功能

时间:2010-07-31 编辑:简简单单 来源:一聚教程网

 代码如下 复制代码





php多文件上传代码









server.php

//upload array files
include 'upload.class.php';
$u = new upload('../uploads/product/','spec','group');
print_r($u->getnewname());
echo $u->geterror();
/***********************
 //upload single file
$u = new upload('../www.111com.net/product/','spec');
print_r($u->getnewname());
$u = new upload('../mb.111com.net/product/','manual');
print_r($u->getnewname());
echo $u->geterror();
************************/
?>

 

 
upload.class.php

 

 代码如下 复制代码

class upload{
    private $savedir="./";
    private $field='new';
    private $allowed=array('.jpg','.jpeg','.gif','.png','.bmp','.rar','.zip','.pdf','.swf');
    private $max =2097152;//单位(字节)
    public $error = '';
    public $newname =array();
 function __construct($dir,$fieldname,$type='single'){
    //var_dump($_files);
    if(!empty($fieldname)){$this->field=$fieldname;}
    //$c = count($_files[$this->field]['tmp_name']);
    //echo $c;
    if(!empty($dir)){$this->savedir=$dir;}

 if($type!='single'){$this->uploadbygroup();
 }else{
  $this->uploadbysingle();
 }
 //print_r($array);

 }
private function uploadbygroup(){
 $array = array();
    foreach($_files[$this->field]['tmp_name'] as $k=>$v){
  $file_ext = strtolower(strrchr($_files[$this->field]["name"][$k], "."));
  $file_size = $_files[$this->field]['size'][$k];
  if($this->isallowedtype($file_ext)&&$this->issizeallowed($file_size)){
  $file_name = time().$this->field.$k;
  move_uploaded_file($_files[$this->field]["tmp_name"][$k], $this->savedir.$file_name.$file_ext);
  $array[] = $file_name.$file_ext;
  }else{
   $this->error = 'file type not allowed or file is too big!';
   return false;
  }
    }
  $this->newname = $array;
 }
private function uploadbysingle(){
 //var_dump($_files);
 $array = array();

  $file_ext = strtolower(strrchr($_files[$this->field]["name"], "."));
  $file_size = $_files[$this->field]['size'];
  if($this->isallowedtype($file_ext)&&$this->issizeallowed($file_size)){
  // $fileprename = substr($_files[$this->field]['name'][$k],0,-strlen($file_ext));//比如原来是22.jpg那就获取22
  //$file_name = time().$fileprename;
  $file_name = time().$this->field;
  move_uploaded_file($_files[$this->field]["tmp_name"], $this->savedir.$file_name.$file_ext);
  $array[] = $file_name.$file_ext;
  }else{
   $this->error = 'file type not allowed or file is too big!';
   return false;
  }

  $this->newname = $array;
  //print_r($this->newname);
 }

 function getnewname(){
  return $this->newname;
 }
 function isallowedtype($type){
  return in_array($type,$this->allowed) ? true : false;
 }
 function issizeallowed($size){
  return $size < $this->max ? true :false;
 }
 function geterror(){
  return $this->error;
 }
}
?>

热门栏目