/* Single file upload feature upload file configuration allowed suffix configuration allowed size get file suffix judge file suffix error */class uptool{protected $allowExt = ' Jpg,jpeg,gi F,bmp,png '; protected $maxSize = 1; 1M, protected $file = NULL in M units; Prepare to store upload file information protected $errno = 0; Error code protected $error = Array (0=> ' no error ', 1=> ' upload file size exceeds system limit ', 2=> ' upload file The size exceeds the page form limit ', 3=> ' file is only partially uploaded ', 4=> ' no files are uploaded ', 6=> ' cannot find Temp folder ', 7 = > ' File write Failed ', 8=> ' disallowed file suffix ', 9=> ' file size exceeds the allowable range of class ', 10=> ' failed to create directory ', 11=> ' file move failed '); /* Upload */Public function up ($key) {if (!isset ($_files[$key])) {return false; } $f = $_files[$key]; Verify that the upload was successful if ($f [' ERROR ']) {$this->errno = $f [' ERROR ']; return false; }//Get suffix $ext = $this->getext ($f [' name ']); Check the suffix if (! $this->isallowext ($ext)) {$this->errno = 8; return false; }//Check size if (! $this->isallowsize ($f [' size '])) {$this->errno = 9; return false; }//Create directory $dir = $this->mk_dir (); if ($dir = = False) {$this->errno = 10; return fasle; }//Generate random file name $newname = $this->randname (). '. '. $ext; $dir = $dir. '/'. $newname; Move if (!move_uploaded_file ($f [' tmp_name '), $dir. '/'. $newname)) {$this->errno = 11; return false; } return True;//str_replace (ROOT, ", $dir); } public Function Geterr () {return $this->error[$this->errno]; }/* Parm string $exts allowed suffixes to automatically add the allowed suffixes, and the size of the file */Public Function Setext ($exts) {$thi S->allowext = $exts;The Public Function setSize ($num) {$this->maxsize = $num; }/* String $file return string $ext suffix */protected function Getext ($file) { $tmp = Explode ('. ', $file); Return end ($TMP); }/* string $ext file suffix return bool prevents case-sensitive issues */protected function Isallowext ($ext) { Return In_array (Strtolower ($ext), explode (', ', Strtolower ($this->allowext)); }/* Check the size of the file */protected function isallowsize ($size) {return $size <= $this->ma Xsize *1024*1024; }//method to create a directory by date protected function Mk_dir () {$dir = ' images/'. Date (' ym/d '); if (Is_dir ($dir) | | mkdir ($DIR, 0777,true)) {return $dir; } else {return false; }/* Generates a random file name */protected function randname ($length = 6) {$str = ' abcdefghijkmn pqrstuvwxyz23456789 '; Return substr (Str_shuffle ($STR), 0, $length); } }
Form form
<form action= "up.php" method= "post" enctype= "Multipart/form-data" > user name: <input type= "text" Name= " Username "/> <br/> Avatar: <input type=" file "name=" pic "/> <input type=" Submit "value=" Submit " /> </form>
Another page call
Require ('./uptool.class.php '); $uptool = new Uptool (); $uptool->setext (' Rar,doc '); $uptool->setsize (1); if ($uptool->up (' pic ')) { echo ' upload succeeded '; } else { echo ' failed '; echo $uptool->geterr (); }
PHP File Upload Class code