不多说直接上代码,支持随机命名,路径,文件大小,后缀限制
 
后端调用实例
 
            
<?php
  class UploadFile{ //完整实例 $upload = new UploadFile(true, '../yuming/', 'xls|xlsx'); 随机命名、路径、允许的后缀
    
	private $max_size   = '8000000'; //设置上传文件的大小,此为8M
    
    public function __construct($rand_name, $save_path, $allow_type){
      $this->rand_name = $rand_name;
      $this->save_path = $save_path;
      $this->allow_type = $this->get_allow_type($allow_type);
    }
    
	public function upload_file($file){
		
      $this->file_name   = $file['name'];
      $this->file_size   = $file['size'];
      $this->error     = $file['error'];
      $this->file_tmp_name = $file['tmp_name'];
      
      $this->ext = $this->get_file_type($this->file_name);
      
      switch($this->error){
        case 0: $this->msg = ''; break;
        case 1: $this->msg = '超出了php.ini中文件大小'; break;
        case 2: $this->msg = '超出了MAX_FILE_SIZE的文件大小'; break;
        case 3: $this->msg = '文件被部分上传'; break;
        case 4: $this->msg = '没有文件上传'; break;
        case 5: $this->msg = '文件大小为0'; break;
		case 6: $this->msg = '找不到临时文件夹'; break;
		case 7: $this->msg = '文件写入失败'; break;
        default: $this->msg = '上传失败'; break;
      }
	  
      if($this->error==0 && is_uploaded_file($this->file_tmp_name)){
        //检测文件类型
        if(in_array($this->ext, $this->allow_type)==false){
          $this->msg = '文件类型不正确';
          return false;
        }
        //检测文件大小
        if($this->file_size > $this->max_size){
          $this->msg = '文件过大';
          return false;
        }
		
		$this->set_file_name();
		$this->uploaded = $this->save_path.$this->new_name;
		
		if(move_uploaded_file($this->file_tmp_name, $this->uploaded)){
			$this->msg = '文件上传成功';
			return true;
		}else{
			$this->msg = '文件上传失败';
			return false;
		}
      }
    }
    
    /**
    * 设置上传后的文件名
    * 当前的毫秒数和原扩展名为新文件名
    */
    public function set_file_name(){
      if($this->rand_name==true){
        $a = explode(' ', microtime());
        $t = $a[1].($a[0]*1000000);
        $this->new_name = $t.'.'.($this->ext);
      }else{
        $this->new_name = $this->file_name;
      }
    }
    
    /**
    * 获取上传文件类型
    * @param string $filename 目标文件
    * @return string $ext 文件类型
    */
    public function get_file_type($filename){
      $ext = pathinfo($filename, PATHINFO_EXTENSION);
      return $ext;
    }
    
    /**
     * 获取可上传文件的类型
     */
    public function get_allow_type($allow_type){
      $s = array();
      $s = preg_split("/[\s,|;]+/", $allow_type);
      return $s;
    }
    
    //获取错误信息
    public function name(){
      return $this->new_name;
    }
	
	public function msg(){
      return $this->msg;
    }
	
	public function error(){
      return $this->error;
    }
  }
后端调用实例
require '../up.php'; //引入上面文件
	$file = $_FILES['file']; //取得传入文件
	$upload = new UploadFile(true, '../upl/', 'xls|xlsx'); //随机命名开启,保存路径,允许的文件类型
	$upload->upload_file($file); //上传开始
	if($upload->error() > 0){ //错误提取
		echo "Error: " . $upload->msg();
	}else{
		
		$xlsname=$upload->name(); //上传成功,文件名
}
                 




