Very classic PHP File Upload class sharing,
File UploadIt is a common function in project development, but the file upload process is cumbersome. As long as there is a file upload, you need to write these complex code. In order to reduce the difficulty of writing functions during each development and reduce the development time, we usually encapsulate the code that is repeatedly used into a class.
<? Php/** + upload * File Upload class + ------------------------------------------------------------------------------- * @ author Administrator + upload */class FileUpload {private $ filepath; // save path private $ allowtype = array ('gif', 'jpg ', 'jpeg', 'png ', 'txt'); private $ maxsize = 10 00000; // maximum allowed upload size private $ israndname = true; // random private $ orginame; // original file name private $ tmpname; // temporary file name private $ newname; // new file name private $ filetype; // file type private $ filesize; // file size private $ errornum = ''; // error code private $ errormsg; // error message/** + response * constructor + -------------------------------------------------------------------------- ---- * @ Param string $ savepath storage path * @ param string $ allowtype allowed type * @ param string $ maxsize allowed size * + allowed */function _ construct ($ option = array ()) {foreach ($ option as $ key => $ value) {if (! In_array ($ key, get_class_vars (get_class ($ this) {continue;} $ this-> setOption ($ key, $ value) ;}} function uploadfile ($ field) {$ return = true; if (! $ This-> CheckPath () {$ this-> errormsg = $ this-> geterrorNum (); return false ;} $ name = $ _ FILES [$ field] ['name']; $ tmpname = $ _ FILES [$ field] ['tmp _ name']; $ filesize = $ _ FILES [$ field] ['SIZE']; $ error = $ _ FILES [$ field] ['error']; if (is_array ($ name )) {$ errors = array (); for ($ I = 0; $ I <count ($ name); $ I ++) {if ($ this-> getFile ($ name [$ I], $ tmpname [$ I], $ filesize [$ I], $ errors [$ I]) {if (! $ This-> CheckSize ()&&! $ This-> CheckType () {$ errors = $ this-> getErrorNum (); return false ;}} else {$ errors = $ this-> getErrorNum (); return false;} if (! $ Return) {$ this-> getFile () ;}} if ($ return) {$ fileNames = array (); for ($ I = 0; $ I <count ($ name); $ I ++) {if ($ this-> getFile ($ name [$ I], $ tmpname [$ I], $ filesize [$ I], $ filesize [$ I]) {$ this-> SetFileName (); if (! $ This-> MoveFile () {$ errors [] = $ this-> getErrorNum (); $ return = false ;} else {$ fileNames [] = $ this-> getNewName () ;}}$ this-> newname = $ fileNames;} $ this-> errormsg = $ errors; return $ return;} else {if ($ this-> getFile ($ name, $ tmpname, filesize, $ error) {if (! $ This-> CheckSize () {return false;} if (! $ This-> CheckType () {return false;} $ this-> SetFileName (); if ($ this-> MoveFile () {return true ;}} else {return false;} if (! $ Return) {$ this-> setOption ('errornum', 0); $ this-> errormsg = $ this-> geterrorNum ();} return $ return ;}} /** + functions * set the class Property value function + functions * @ param mix $ key * @ param mix $ value */private function setOption ($ key, $ value) {$ key = strtolower ($ key); $ this-> $ key = $ value;}/** + --- Required * get the file Variable Parameter function + required * @ param string $ name * @ param string $ tmp_name * @ param number $ size * @ param number $ error */private function getFile ($ name, $ tmpname, $ filetype, $ filesize, $ error = 0) {$ this-> setOption ('tmpname', $ TmpName); $ this-> setOption ('orginame ', $ n Ame); $ arrstr = explode ('. ', $ name); $ this-> setOption ('filetype', $ arrstr [count ($ arrstr)-1]); $ this-> setOption ('filesize ', $ filesize); return true;}/** + ------------------------------------------------------------------------- * Check the upload path function + callback * @ return boolean */private function CheckPath () {if (empty ($ this-> filepath) {$ this-> s EtOption ('errornum',-5); return false;} if (! File_exists ($ this-> filepath) |! Is_writable ($ this-> filepath) {if (! @ Mkdir ($ this-> filepath, 0755) {$ this-> setOption ('errornum',-4); return false ;}} return true ;} private function Is_Http_Post () {if (! Is_uploaded_file ($ this-> tmpname) {$ this-> setOption ('errornum',-6); return false;} else {return true ;}} /** + -------------------------------------------------------------------- * check the file size function + -------------------------------------------------------------------- * @ return boolean */private function CheckSize () {if ($ this-> filesize> $ this-> maxsize) {$ this-> setOption ('errornum',-2); return false;} el Se {return true;}/** + ----------------------------------------------------------------- * check file type functions + functions * @ return boolean */private function CheckType () {if (in_array ($ this-> filetype, $ this-> allowtype) {return true;} else {$ this-> setOption ('errornum',-1 ); return false ;}} private function SetFileName () {if ($ this-> israndname) {$ th Is-> setOption ('newname', $ this-> RandName ();} else {$ this-> setOption ('newname', $ this-> orginame );}} /** + response * Get new file name + ------------------------------------------------------------------ */public function getNewName () {return $ this-> newname;} private function RandName () {$ rule = date ("YmdHis "). rand (0,999); return $ rule. '. '. $ this-> file Type;} private function MoveFile () {if ($ this-> errornum) {$ filepath = rtrim ($ this-> filaepath ,'/'). '/'; $ filepath. = $ this-> newname; if (@ move_uploaded_file ($ this-> tmpname, $ filepath) {return true ;} else {$ this-> errormsg = $ this-> setOption ('errornum',-3) ;}} else {return false ;}} /** + ---------------------------------------------------------------- * error message function + ---------------------------------------- ------------------------ * @ Return string */function getErrorNum () {$ erstr = "An error occurred while uploading a file <font color = 'red' >{$ this-> orginame} </font> "; switch ($ this-> errornum) {case 4: $ erstr. = "no file is uploaded"; break; case 3: $ erstr. = "the file is only partially uploaded"; break; case 2: $ erstr. = "the uploaded file exceeds the value specified by the HTML form MAX_FILE_SIZE"; break; case 1: $ erstr. = "the File Uploaded exceeds php. the value of upload_max_filesize in the ini configuration file "; break; case 0: $ erstr =" Upload {$ this-> orginame} succeeded "; break; case -1: $ erstr = "unsupported type"; break; case-2: $ erstr. = "the file is too large to exceed {$ this-> maxsize} bytes"; break; case-3: $ erstr. = "Upload Failed"; break; case-4: $ erstr = "failed to create the upload directory. Please specify the upload directory again"; break; case-5: $ erstr = "unspecified upload path"; break; case-6: $ erstr = "invalid operation"; break; default: $ erstr. = "Unknown error";} return $ erstr ;}}?>
The above is all the content of this article. I hope it will help you learn php programming.