This example for everyone to share the PHP file upload class, the function is very powerful, for your reference, the specific content as follows
<? php/** file Upload class **/class upfile{private $file _size;//upload source file size private $file _tem;//upload file temporary store name private $file _name;// Upload filename private $file _type;//upload file type private $file _max_size=2000000;//allow file upload maximum private $file _folder= "uploadfiles";// File upload path Private $over _write=false;//whether to overwrite files with the same name//allow uploading of picture types private $allow _type=array (' image/jpg ', ' image/jpeg ', ' image/ PNG ', ' image/pjpeg ', ' image/gif ', ' image/bmp ', ' image/x-png '); Construct class, File: Upload domain function __construct ($file) {$this->file_name=$_files[$file] [' Name '];//client original file name $this->file_ type=$_files[$file] [' type '];//file type $this->file_tem=$_files[$file] [' Tmp_name '];//the temporary file name stored, typically the system default $this-> file_size=$_files[$file [' Size '];//file sizes}//If the folder does not exist create a folder function Creatfolder ($f _path) {if (!file_exists ($f _path)) {mkdir ($f _path,0777);}} Determine if the file is not over the upload limit function is_big () {if ($this->file_size> $this->file_max_size) {echo "file is too large to exceed the limit!) "; exit;}} Check file type function Check_type () {if (!in_array ($this->file_type, $this->allow_type)) {echo "Upload file type is incorrect"; exit;}}// Check if file exists function Check_file_naMe () {if (!file_exists ($this->file_tem)) {echo "Upload file does not exist"; exit;} }//Check if there is a file with the same name, overwrite function check_same_file ($filename) {if (file_exists ($filename) && $this->over_write!= true) {echo "file with the same name already exists! "; exit;}} Move File function Move_file ($filename, $destination) {if (!move_uploaded_file ($filename, $destination)) {echo "Error moving File"; Exit;}} Check if the file is an HTTP POST upload function is_upload_file () {if (!is_uploaded_file ($this->file_tem)) {echo "file does not exist"; exit;}}// Get file suffix function getext () {$ext = $this->file_name; $extstr =explode ('. ', $ext); $count =count ($EXTSTR) -1;return $ extstr[$count];} New file name function Set_name () {return time (). ".". $this->getext ();} Set the date of the year as the folder name function Creat_mulu () {$this->creatfolder (". /.. /upload/". Date (YMD)); Return "upload/". Date (YMD);} The generated file name function Files_name () {$name = $this->set_name (); $folder = $this->creat_mulu (); return ". /.. /". $folder." /". $name;} Upload file function upload_file () {$f _name= $this->files_name (); Move_uploaded_file ($this->file_tem, $f _name); RetuRN $f _name;} Generate thumbnails//maximum width: 120, High: 120function create_simg ($img _w, $img _h) {$name = $this->set_name (); $folder = $this->creat_ Mulu (); $new _name= ". /.. /". $folder." /s_ ". $name; $imgsize =getimagesize ($this->files_name ()); Switch ($imgsize [2]) {Case 1:if (!function_exists ("Imagecreatefromgif")) {echo "Your GD library cannot use pictures in GIF format, please use JPEG or PNG format!) Return "; exit ();} $im = Imagecreatefromgif ($this->files_name ()), Break;case 2:if (!function_exists ("Imagecreatefromjpeg")) {echo] Your GD library can not use JPEG format pictures, please use other format pictures! Return "; exit ();} $im = Imagecreatefromjpeg ($this->files_name ()) Break;case 3: $im = Imagecreatefrompng ($this->files_name ()); Break;case 4: $im = Imagecreatefromwbmp ($this->files_name ()) Break;default:die ("Isn't filetype right"); exit;} $SRC _w=imagesx ($im);//Obtain Image width $src_h=imagesy ($im),//Get Image Height $new_wh= ($img _w/$img _h);//ratio of new image width to height $src_wh= ($src _w/$ SRC_H);//The ratio of the original image width to height if ($new _wh<= $src _wh) {$f _w= $img _w; $f _h= $f _w* ($src _h/$src _w);} else{$f _h= $img _h $f _w= $f _h* ($src _w/$src _h);} if ($src _w> $img _w| | $SRC _h> $img _h) { if (function_exists ("Imagecreatetruecolor")) {//check whether the function is defined $new_img=imagecreatetruecolor ($f _w, $f _h); if ($new _img) { Imagecopyresampled ($new _img, $im, 0,0,0,0, $f _w, $f _h, $src _w, $src _h);//Resample copy part of image and resize}else{$new _img=imagecreate ($f _w, $f _h); imagecopyresized ($new _img, $im, 0,0,0,0, $f _w, $f _h, $src _w, $src _h);}} else{$ $new _img=imagecreate ($f _w, $f _h) imagecopyresized ($new _img, $im, 0,0,0,0, $f _w, $f _h, $src _w, $src _h);} if (function_exists (' imagejpeg ')) {imagejpeg ($new _img, $new _name);} Else{imagepng ($new _img, $new _name);} Imagedestroy ($new _img);} Imagedestroy ($new _img); return $new _name;} }?>
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.