PHP Image Upload class (with thumbnails added ),
The thumbnail function is not comprehensive, and it is a problem. Continue to learn and modify it later.
<Form action = "<? Php $ _ SERVER ['php _ SELF '];?> "Enctype =" multipart/form-data "method =" post "> <input type =" text "name =" name "/> <input type =" file "name =" file "/> <input type =" submit "name = 'submit 'value =" submit "> </form> <? Php/*** Created by PhpStorm. * User: Administrator * Date: 2016/6/28 * Time: 21:04 */class upload {protected $ fileMine; // File upload type: protected $ filepath; // File upload path: protected $ filemax; // file upload size: protected $ fileExt; // File Upload format: protected $ filename; // file name: protected $ fileerror; // file error setting: protected $ fileflag; // file detection protected $ fileinfo; // FILES protected $ ext; // file extension protected $ path; // File Upload public function _ construct ($ Filename = "file", $ filemax = 20000000, $ filepath = ". /Uploads ", $ fileflag = true, $ fileExt = array ('jpg ', 'exe'), $ fileMine = array ('image/jpeg ')) {$ this-> filename = $ filename; $ this-> fileinfo = $ _ FILES [$ this-> filename]; $ this-> filemax = $ filemax; $ this-> filepath = $ filepath; $ this-> fileflag = $ fileflag; $ this-> fileExt = $ fileExt; $ this-> fileMine = $ fileMine; // var_dump ($ this-> filename);} // public function UpError () {if ($ This-> fileinfo ['error']> 0) {switch ($ this-> fileinfo ['error']) {case 1: $ this-> fileerror = "the size of the uploaded file exceeds the maximum size allowed by the server. php. set the value restricted by the upload_max_filesize option in ini "; break; case 2: $ this-> fileerror =" the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form "; break; case 3: $ this-> fileerror = "partially uploaded files"; break; case 4: $ this-> fileerror = "no file selected"; break; case 5: $ this-> fileerror = "temporary directory not found"; break; case 6: $ this-> fileerror = "file write failed"; break; Case 7: $ this-> fileerror = "php file upload extension not enabled"; break; case 8: $ this-> fileerror = ""; break;} return false ;} return true;} // check the file type public function UpMine () {if (! In_array ($ this-> fileinfo ['type'], $ this-> fileMine) {$ this-> error = "the File Upload type is incorrect"; return false ;} return true;} // check the file format public function UpExt () {$ this-> ext = pathinfo ($ this-> fileinfo ['name'], PATHINFO_EXTENSION ); // var_dump ($ ext); if (! In_array ($ this-> ext, $ this-> fileExt) {$ this-> fileerror = "File Format incorrect"; return false;} return true ;} // check the file path public function UpPath () {if (! File_exists ($ this-> filepath) {mkdir ($ this-> filepath, 0777, true) ;}// check the file size public function UpSize () {$ max = $ this-> fileinfo ['SIZE']; if ($ max> $ this-> filemax) {$ this-> fileerror = "the file is too large "; return false;} return true;} // check whether the file is HTTP public function UpPost () {if (! Is_uploaded_file ($ this-> fileinfo ['tmp _ name']) {$ this-> fileerror = "malicious repayment"; return false;} return true ;} // file name prevent repeated public function Upname () {return md5 (uniqid (microtime (true), true);} // image thumbnail public function Smallimg ($ x = 100, $ y = 100) {$ imgAtt = getimagesize ($ this-> path); // image width, height, type $ imgWidth = $ imgAtt [0]; $ imgHeight = $ imgAtt [1]; $ imgext = $ imgAtt [2]; // proportional column scaling if ($ x/$ imgWidth)> ($ y/$ imgHeight) {$ bl = $ y/$ imgH Eight;} else {$ bl = $ x/$ imgWidth;} $ x = floor ($ imgWidth * $ bl ); // scale $ y = floor ($ imgHeight * $ bl); $ images = imagecreatetruecolor ($ x, $ y); $ big = imagecreatefromjpeg ($ this-> path ); imagecopyresized ($ images, $ big, 0, 0, 0, $ x, $ y, $ imgWidth, $ imgWidth); switch ($ imgext) {case 1: $ imageout = imagecreatefromgif ($ this-> path); break; case 2: $ imageout = imagecreatefromjpeg ($ this-> path); break; case 3: $ imageout = imagecreatefrom Gif ($ this-> path); break;} $ im = imagejpeg ($ images, $ this-> path);} // File Double-pass public function uploads () {if ($ this-> UpError () & $ this-> UpMine () & $ this-> UpExt () & $ this-> UpSize () & $ this-> UpPost () {$ this-> UpPath (); $ names = $ this-> Upname (); $ this-> path = $ this-> filepath. '/'. $ names. '. '. $ this-> ext; if (move_uploaded_file ($ this-> fileinfo ['tmp _ name'], $ this-> path) {return $ this-> path ;} else {$ this-> fileerror = "Upload loss Failed ";}} else {exit (" <B> ". $ this-> fileerror." </B> ") ;}}?>
<?php header("content-type:imagejpeg");header("Content-type:text/html;charset=utf-8"); require 'list.php'; $u=new upload(); $a=$u->uploads(); $c=$u->Smallimg();echo "
<! DOCTYPE html>