Practical file upload class in php_PHP tutorial

WBOY
Release: 2016-07-13 16:55:02
Original
880 people have browsed it

class upload{
    /**
* The name attribute of the html form input field, the default is 'file'
​*/
    var $file_field = 'file' ;
   
    /**
* $_files array
​*/
    var $file_array;
   
    /**
* * Save path, the default is the current path
​*/
    var $save_path = '';
   
    /**
* Custom file name
​*/
    var $define_name;   
   
    /**
* The final saved file name
​*/
    var $name;
   
    /**
* File size, unit: bytes
​*/
    var $size;
   
    /**
* File extension, excluding "."
​*/
    var $ext;    
   
    /**
* * File types allowed to be uploaded, no restrictions by default
​*/
    var $allow_ext = array();
   
    /**
* * File types allowed to be uploaded, no restrictions by default
​*/
    var $allow_size = false;
   
    /**
* If a file with the same name already exists, whether to allow overwriting, the default is not to overwrite.
​*/
    var $overwrite = false;
   
    /**
* Error message
​*/
    var $error_code;
   
    /**
* Constructor
​*/
    public function __construct(){
        if(!is_uploaded_file($_files[$this->file_field]['tmp_name'])){
            die("非法上传!");
        }else{
            $this->file_array = $_files[$this->file_field];
            $this->name = $this->getpro('name');
            $this->size = $this->getpro('size');
            $this->ext = $this->getpro('ext');   
        }
    }
   
   
    /**
* Upload operation function
* @abstract Return true if the upload is successful, otherwise return the corresponding error code
* @return string or bool
​*/
    public function upload(){
        if(is_uploaded_file($this->file_array['tmp_name'])){   
               
            if(!empty($this->allow_ext)){
                if(!in_array($this->ext,$this->allow_ext)){
                    $this->error_code = "不允许上传该类型文件!";   
                    return false;
                }
            }
           
            if(!@file_exists(iconv('utf-8','gbk',$this->save_path))){
                      $this->error_code = "The file upload directory does not exist!";
                     return false;
            }
                                                                    If(!is_writable(iconv('utf-8','gbk',$this->save_path))){
                    $this->error_code = "The file upload directory cannot be written!";
                     return false;
            }
                                                                    If($this->overwrite==false && @file_exists(iconv('utf-8','gbk',$this->save_path.$this->name))){
                                             $this->error_code = "The file already exists!";
                      return false;
            }
                                                                    If($this->allow_size){
If($this->size > $this->allow_size){
​​​​​​​​​​                            return false;
                }
            }
                                                                    $result = @move_uploaded_file($this->file_array['tmp_name'],iconv("utf-8","gbk",$this->save_path.$this->getpro("name"))) ;                            
                if($result){
                    return true;
               }else{
switch($this->file_array['error']){
                 case 1:
                                                                                                                                                                                                                        $this->error_code = "The uploaded file exceeds the value of the upload_max_filesize option limit!";
break;
                 case 2:
$this->error_code = "The size of the uploaded file exceeds the value specified by the max_file_size option!";
break;
                  case 3:
​​​​​​​​​​​​ break;
case 4:
​​​​​​​​​​​ break;
case 6:
                                                                                                                                                                                                                                                                                             through break;
case 7:
$this->error_code = "File writing failed!";
break;
                }
                      return false;
            }
}
}
 
/**
* Upload operation function
* @abstract Get file attributes
* @param $itme: string type, valid inputs are name (file name), ext (file extension), size (file size)
* @return string
​*/
Public function getpro($item){
         switch($item){
            case "name":
$filename = $this->file_array['name'];
                       return isset($this->define_name) ? $this->define_name.".".$this->ext : $filename;
Break;
case "ext":
$filename = $this->file_array['name'];
                      $filter = explode(".",$filename);
                     return strtolower($filter[count($filter)-1]);
             break;
            case "size":
                      return $this->file_array['size'];
             break;
                 default:
die("Illegal operation!");
             break;
}
}
}

http://www.bkjia.com/PHPjc/631718.html

truehttp: //www.bkjia.com/PHPjc/631718.htmlTechArticleclass upload{ /*** The name attribute of the html form input field, the default is 'file'*/ var $file_field = 'file' ; /*** $_files array*/ var $file_array; /** * Save path, default is the current path...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template