<?php/** * Created by Phpstorm. * User:administrator * DATE:2016/5/26 * time:20:29 */class upload{protected $fileName; protected $maxSize; protected $allowMime; protected $allowExt; protected $uploadPath; protected $imgFlag; protected $fileInfo; protected $error; protected $ext; /** * @param string $fileName * @param string $uploadPath * @param string $imgFlag * @param number $maxSiz E * @param array $allowExt * @param array $allowMime */Public function __construct ($fileName = ' photo ', $uplo Adpath= './uploads ', $imgFlag =true, $maxSize =5242880, $allowExt =array (' jpeg ', ' jpg ', ' png ', ' gif '), $allowMime =array (' Image/jpeg ', ' image/png ', ' image/gif ') {$this->filename= $fileName; $this->maxsize= $maxSize; $this->allowmime= $allowMime; $this->allowext= $allowExt; $this->uploadpath= $uploadPath; $this->imgflag= $imgFlag; $this->fileinfo=$_files[$this->filename]; } /** * Detect errors in uploading files * @return Boolean */protected function checkerror () {if (!is_null ($this->fileinfo)) {if ($this->fileinfo[' ERROR ']>0) {switch ($this->fileinfo[' ERROR ']) { Case 1: $this->error= ' exceeds the value of the upload_max_filesize option in the PHP configuration file '; Break Case 2: $this->error= ' exceeds the value of max_file_size set in the form '; Break Case 3: $this->error= ' file is partially uploaded '; Break Case 4: $this->error= ' no option to upload files '; Break Case 6: $this->error= ' no temporary directory found '; Break Case 7: $this->error= ' file is not writable '; Break Case 8: $this->error= ' due to PHP extension interrupt file Upload '; Break } return false; }else{return true; }}else{$this->error= ' file On error '; return false; }}/** * Detects the size of the uploaded file * @return Boolean */protected function checksize () {if ($this->fileinfo [' Size ']> $this->maxsize) {$this->error= ' upload file too large '; return false; } return true; }/** * Detect extension * @return Boolean */protected function Checkext () {$this->ext=strtolower (pathin Fo ($this->fileinfo[' name '],pathinfo_extension)); if (!in_array ($this->ext, $this->allowext)) {$this->error= ' disallowed extensions '; return false; } return true; }/** * Type of Detection file * @return Boolean */protected function Checkmime () {if (!in_array ($this->filei nfo[' type '], $this->allowmime) {$this->error= ' disallowed file type '; Return False } return true; }/** * Detects if it is a real picture * @return Boolean */protected function checktrueimg () {if ($this->imgflag) { if ([email protected] ($this->fileinfo[' Tmp_name ')) {$this->error= ' is not a real picture '; return false; } return true; }}/** * detects if an HTTP post is uploaded to the * @return Boolean */protected function Checkhttppost () {if (! Is_uploaded_file ($this->fileinfo[' Tmp_name ')) {$this->error= ' file is not uploaded via HTTP post]; return false; } return true; }/** * Display error */protected function ShowError () {exit (' <span style= ' color:red ' > '. $this->error. ' </span> '); }/** * Detection directory does not exist then create */protected function Checkuploadpath () {if (!file_exists ($this->uploadpath)) { mkdir ($this->uploadpath,0777,true); }}/** * produces a unique string * @return string */protected function Getuniname () {return MD5 (Uniqid (Microtime (True), true)); /** * Upload file * @return String * * Public Function UploadFile () {if ($this->checkerror () && ; $this->checksize () && $this->checkext () && $this->checkmime () && $this Checktrueimg () && $this->checkhttppost ()) {$this->checkuploadpath (); $this->names= $this->getuniname (); $this->destination= $this->uploadpath. ' /'. $this->names. '. $this->ext; if (@move_uploaded_file ($this->fileinfo[' tmp_name ', $this->destination)) {return $this->destinati On }else{$this->error= ' file move failed '; }}else{$this->showerror (); } }}
PHP Upload Class