- class FileUpload {
- private $filepath; //Specify the path to save the uploaded file
- private $allowtype=array('gif', 'jpg', 'png', 'jpeg'); //Allow uploading files Type
- private $maxsize=1000000; //The maximum length of uploaded files is 1M
- private $israndname=true; //Whether to rename randomly, false is not random, use the original file name
- private $originName; //Source file name
- private $tmpFileName; //Temporary file name
- private $fileType; //File type
- private $fileSize; //File size
- private $newFileName; //New file name
- private $errorNum=0; //Error number
- private $errorMess=""; //Used to provide error reports
-
- //Used to initialize uploaded files
- //1. Specify the upload path, 2. Allowed type, 3. Limit size, 4. Whether to use Random file name
- //Allows users to pass parameters by position, and provide values for subsequent parameters without also providing values for the first few parameters
- function __construct($options=array()){
- foreach($options as $key=> ;$val){
- $key=strtolower($key);
- //Check whether the subscript of the array in the user parameter is the same as the member attribute name
- if(!in_array($key,get_class_vars(get_class($this))) ){
- continue;
- }
- $this->setOption($key, $val);
- }
- }
- private function getError(){
- $str="Upload file Error when {$this->originName}: ";
- switch($this->errorNum){
- case 4: $str .= "No file was uploaded"; break;
- case 3: $str .= "The file was only partially uploaded"; break;
- case 2: $str .= "The uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form"; break;
- case 1: $str .= "The uploaded file Exceeded the value of the upload_max_filesize option in php.ini"; break;
- case -1: $str .= "Unallowed type"; break;
- case -2: $str .= "The file is too large and cannot be uploaded Exceeding {$this->maxSize} bytes"; break;
- case -3: $str .= "Upload failed"; break;
- case -4: $str .= "Failed to create a directory to store uploaded files, please Re-specify the upload directory"; break;
- case -5: $str .= "The path to the uploaded file must be specified"; break;
- default: $str .= "Unknown error";
- }
- return $str.'< ;br>';
- }
- //Used to check the file upload path
- private function checkFilePath(){
- if(empty($this->filepath)) {
- $this->setOption('errorNum', - 5);
- return false;
- }
- if(!file_exists($this->filepath) || !is_writable($this->filepath)){
- if(!@mkdir($this->filepath, 0755)){
- $this->setOption('errorNum', -4);
- return false;
- }
- }
- return true;
- }
- //Used to check the size of file upload
- private function checkFileSize() {
- if($this->fileSize > $this->maxsize){
- $this->setOPtion('errorNum', '-2');
- return false;
- }else{
- return true;
- }
- }
- //Used to check the file upload type
- private function checkFileType() {
- if(in_array(strtolower($this->fileType), $this->allowtype)) {
- return true;
- } else{
- $this->setOption('errorNum', -1);
- return false;
- }
- }
- //Set the uploaded file name
- private function setNewFileName(){
- if($this-> israndname){
- $this->setOption('newFileName', $this->proRandName());
- } else {
- $this->setOption('newFileName', $this->originName);
- }
- }
-
- //Set a random file name
- private function proRandName(){
- $fileName=date("YmdHis").rand(100,999);
- return $fileName.'.'.$this->fileType;
- }
- private function setOption($key, $val){
- $this->$key=$val;
- }
- //Used to upload a file
- function uploadFile($fileField){
- $return=true;
- //Check the file upload path
- if(!$this->checkFilePath()){
- $this->errorMess=$this->getError();
- return false;
- }
- $name=$_FILES [$fileField]['name'];
- $tmp_name=$_FILES[$fileField]['tmp_name'];
- $size=$_FILES[$fileField]['size'];
- $error=$_FILES[$ fileField]['error'];
- if(is_Array($name)){
- $errors=array();
- for($i=0; $iif( $this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
- if(!$this->checkFileSize() | | !$this->checkFileType()){
- $errors[]=$this->getError();
- $return=false;
- }
- }else{
- $error[]=$this-> getError();
- $return=false;
- }
- if(!$return)
- $this->setFiles();
- }
- if($return){
- $fileNames=array();
- for($ i=0; $iif($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error [$i])){
- $this->setNewFileName();
- if(!$this->copyFile()){
- $errors=$this->getError();
- $return=false;
- }else{
- $fileNames[]=$this->newFileName;
- }
- }
- }
- $this->newFileName=$fileNames;
- }
- $this->errorMess=$errors;
- return $return;
- } else {
- if($this->setFiles($name, $tmp_name, $size, $error)){
- if($this->checkFileSize() && $this->checkFileType()){
- $this->setNewFileName();
- if($this->copyFile()){
- return true;
- }else{
- $return=false;
- }
- }else{
- $return=false;
- }
- }else{
- $return=false;
- }
- if(!$return)
- $this->errorMess=$this->getError();
- return $return;
- }
- }
- private function copyFile(){
- if(!$this->errorNum){
- $filepath=rtrim($this->filepath, '/').'/';
- $filepath.=$this->newFileName;
- if(@move_uploaded_file($this->tmpFileName, $filepath)) {
- return true;
- }else{
- $this->setOption('errorNum', -3);
- return false;
- }
- }else{
- return false;
- }
- }
- //设置和$_FILES有关的内容
- private function setFiles($name="", $tmp_name='', $size=0, $error=0){
- $this->setOption('errorNum', $error);
- if($error){
- return false;
- }
- $this->setOption('originName', $name);
- $this->setOption('tmpFileName', $tmp_name);
- $arrStr=explode('.', $name);
- $this->setOption('fileType', strtolower($arrStr[count($arrStr)-1]));
- $this->setOption('fileSize', $size);
- return true;
- }
- //用于获取上传后文件的文件名
- function getNewFileName(){
- return $this->newFileName;
- }
- //上传如果失败,则调用这个方法,就可以查看错误报告
- function getErrorMsg() {
- return $this->errorMess;
- }
- }
复制代码
|