/** * Detect upload Files * @ param array $rule Validation rules * @return bool */ public function check ($rule = []) {// detection upload file $rule = $rule ?: $this->validate;// rule rules get /* Check File size */ if ( Isset ($rule [' size ']) && ! $this->checksize ($rule [' size ']) {// file size $this->error = ' upload file size does not match! '; return false; } /* Check file MIME type */ if (Isset ($rule [' type ']) && ! $this->checkmime ($rule [' type ']) {// File Types $this->error = ' Upload file MIME type not allowed! '; return false; } /* Check file suffix */ if (Isset ($rule [' ext ')] && ! $this Checkext ($rule [' ext ')]) {// file suffix $this->error = ' upload file suffix not allowed '; return false; } /* Check image file */ if (! $this->checkimg ()) { // whether it's a picture $this->error = ' Illegal image file! '; return false; } return true; } /** * detect upload file suffix * @param array|string $ext allow suffix * @return bool */ public Function checkext ($ext) { if ( Is_string ($ext)) { $ext = eXplode (', ', $ext);// get license file suffix } $extension = strtolower (PathInfo ($this->getinfo (' name '), pathinfo_extension); if (!in_array ($extension, $ext)) {// determine the existence of license within the scope of return false; } return true; } /** * Detecting image files * @return bool */ public function checkimg ()// picture detection { $extension = strtolower (PathInfo ($this->getinfo (' name '), pathinfo_extension));   &Nbsp; /* Strict detection of image files */ if (In_array ($extension, [' gif ', ' jpg ', ' jpeg ', ' BMP ', ' png ', ' swf ')) && !in_array ($this->getimagetype ($this->filename), [1, 2, 3, 4, 6] )) { return false; } return true; } // Judging Image Type protected function getimagetype ($image)// picture type detection { if (function_exists (' Exif_imagetype ')) { return exif_imagetype ($image); } else { $info = getimagesize ($image); return $info [2]; } } /** * detect upload file size * @param integer $size Maximum size * @return bool */ public function checksize ($size) { if ($this->getsize () > $size) {// File Size return false; } return true; } /** * detecting upload file types * @param array| string $mime allowed types * @return Bool */ public function checkmime ($mime) { if (is_string ($mime)) { $mime = explode (', ', $mime); } if (!in_ Array (Strtolower ($this->getmime ()), $mime)) {// file type License return false; } return true; } /** * moving files * @param string $path Save path * @param string|bool $savename saved filenames Default auto-Generate * @param boolean $replace Overwrite files with same name * @return false| Splfileinfo false-failed returned Splfileinfo instance */ public function move ($path, $savename = true, $replace = true) {// Save Path Error name // file upload failed, catch fault code if (!empty ($this->info[' error ')) {// capture Error message Code $this->error ($this->info[' error '); return false; } // detection Legality if (! $this->isvalid ()) {// legitimacy $this->error = ' illegally uploading files '; return false; } // Verification uploads if (!$ This->check ()) {// verification upload return false; } $path = rtRim ($path, ds) . DS;// delete right delimiter File Save naming rules $saveName = $this->buildsavename ( $savename);// get saved file name $filename = $path . $saveName;// generate file name // detect directory if (false === $this->checkpath (dirname ($filename))) {// Inspection Catalogue return false; } /* Do not overwrite files with the same name */ if (! $replace && Is_file ($filename)) {// file with the same name overwrite $this->ERROR&Nbsp;= ' exists file of the same name ' . $filename; return false; } /* Mobile Files */ if ($this->istest) { rename ($this->filename, $filename);// moving files } elseif (!move_ Uploaded_file ($this->filename, $filename)) {// normal upload file $this->error = ' File Upload Save error! '; return false; } // returning file Object instances $file = new Self ($filename);// after a series of error processing $file Setsavename ($saveName);// settings error files $file Setuploadinfo ($this->info);// save Info to information return $file;// return instantiation handle } /** * get save file name * @param string|bool $savename saved file name Default auto-Generate * @return string */ protected function buildsavename ($savename) { if (true === $savename) { // automatically generate file names if ($this->rule instanceof \closure) { $savename = call_user_ Func_array ($this->rule, [$this]);// get file name } else { switch ($this->rule) { case ' MD5 ': $md 5 = md5_file ($this->filename); $savename = substr ($md 5, 0, 2) . ds . substr ($md 5, 2);// File name of md5 mode break; case ' SHA1 ': $sha 1 = sha1_file ($this->filename); $savename = substr ($sha 1, 0, 2) . ds . substr ($sha 1,  2);// sha1 mode file name break; case ' Date ': $ Savename = date (' Ymd ') . ds . md5 (Microtime (True));// date mode file name break; default: $savename = call_user_func ($this->rule);// call rule function generate file name } } } elseif (" === $savename) { $savename = $this->getinfo (' name ');// Empty } if (! Strpos ($savename, '. ')) { $savename .= '. ' . pathinfo ($this->getinfo (' name '), pathinfo_extension);// has its own specified file name } return $savename; } /** * get error code information * @param int $errorNo Error number */ private function error ($errorNo)// error message translation { switch ($errorNo) { case 1: case 2: $this->error = ' upload file size exceeds the maximum value! '; break; case 3: $this->error = ' files are only partially uploaded! '; break; case 4: $this->error = ' no files were uploaded! '; break; case 6: $this->error = ' can't find the Temp folder! '; break; case 7: $this->error = ' file failed to write! '; break; default: $this->error = ' unknown upload error! '; } } /** * get error messages * @return mixed */ public function geterror ()// return error message { return $this->error; }}
This article is from the "Focus on PHP Group number: 414194301" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1877990
[Li Jingshan php] every day tp5-20170105|thinkphp5-file.php-2