<?phpclass Upload {private $error; Private $destination; Private $fileInfo; Private $uploadDir; Private $maxFileSize; Private $allowExt; Private $checkImage; function __construct ($fileInfo, $uploadDir = './uploads ', $maxFileSize = 1024x768 * 1024x768 * 1, $allowEx t = array (' jpg ', ' gif ', ' PNG ', ' BMP ', ' jpeg '), $checkImage = true, $allowMime = Array (' Image/gif ', ' Image/jpeg ', ' image/png ') {$this->fileinfo = $fileInfo; $this->uploaddir = $uploadDir; $this->maxfilesize = $maxFileSize; $this->allowext = $allowExt; $this->checkimage = $checkImage; $this->allowmime = $allowMime; /*** * Processing Upload file * @return String */Public Function UploadFile () {if (! $this->fileinfo) { $this->error = ' File information does not exist '; return $this->error; if ($this->checkerror () && $this->checkimagetype () && $tHis->checkmaxfilesize () && $this->checkallowext () && $this->checkpostupload () &&am P $this->checkmime ()) {//Determine if the folder exists $this->checkuploaddir (); Generates a unique file name $destination = $this->uploaddir. ‘/‘ . $this->builduniqueid (). ‘.‘ . $this->getext (); if (@move_uploaded_file ($this->fileinfo[' tmp_name '), $destination) {$this->destination = $destinati On return $this->destination; } else {$this->error = ' Move upload file failed '; } return $this->error; } return $this->error; } Private Function CheckError () {if ($this->fileinfo[' error '] = = UPLOAD_ERR_OK) {return true; } $this->error = ' upload error, error code: '. $this->fileinfo[' ERROR ']; return false; } Private Function Builduniqueid () {return MD5 (Uniqid (Microtime (True), true)); } Private Function Checkuploaddir () {if (!file_exists ($this->uploaddir)) {mkdir ($this->up Loaddir, 0777, true); chmod ($this->uploaddir, 0777); }} Private Function Checkallowext () {if (In_array ($this->getext (), $this->allowext)) {RET Urn true; } $this->error = ' file of this type cannot be uploaded '; return false; } Private Function Checkmaxfilesize () {if ($this->maxfilesize > $this->fileinfo[' size ') { return true; } $this->error = ' the size of the uploaded file has exceeded the limit '; return false; } Private Function Checkimagetype () {if ($this->checkimage) {if ($this->checkallowext () & & GetImageSize ($this->fileinfo[' Tmp_name ')) {return true; } $this->error = ' upload is not a picture '; return false; } return true; } Private Function Checkpostupload () {if (Is_uploaded_file ($this->fileinfo[' Tmp_name ')) {return true; } $this->error = ' is not a post-uploaded file '; return false; } Private Function Checkmime () {if (In_array ($this->getmime (), $this->allowmime)) {return Tru E } $this->error = ' MIME type incorrect '; return false; } Private Function Getmime () {if ($imageInfo = getimagesize ($this->fileinfo[' Tmp_name ')) {retur n $imageInfo [' MIME ']; } return false; } Private Function Getext () {return strtolower (pathinfo ($this->fileinfo[' name '], pathinfo_extension)); }}
PHP File Upload class