: This article mainly introduces PHP's super easy-to-use file Upload processing class 1 (37). If you are interested in PHP tutorials, please refer to it.
Array () {foreach ($ options as $ key => $ val) {$ key = strtolower ($ key ); // check whether the subscript of the array in the user parameter is the same as the member attribute name if (! In_array ($ key, get_class_vars (get_class ($ this) {continue;} $ this-> setOption ($ key, $ val);} private function getError () {$ str = "error when uploading file {$ this-> originName}:"; switch ($ this-> errorNum) {case4: $ str. = "no file is uploaded"; break; case3: $ str. = "The file is only partially uploaded"; break; case2: $ str. = "the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form"; break; case1: $ str. = "The file uploaded exceeds php. value of the upload_max_filesize option in ini "; break; case-1: $ str. = "Last supported types"; break; case-2: $ str. = "The file is too large to upload more than {$ this-> maxSize} bytes"; break; case-3: $ str. = "Upload failed"; break; case-4: $ str. = "an error occurred while creating the Upload file directory. please specify the upload Directory Again"; break; case-5: $ str. = "the path of the file to be uploaded must be specified"; break; default: $ str. = "last known error";} return $ str.'
';}// Used to check the file upload path private function checkFilePath () {if (empty ($ this-> filepath) {$ this-> setOption ('errornum ', -5); returnfalse;} if (! File_exists ($ this-> filepath) |! Is_writable ($ this-> filepath) {if (! @ Mkdir ($ this-> filepath, 0755) {$ this-> setOption ('errornum',-4); returnfalse ;}} returntrue ;} // check the file upload size. private function checkFileSize () {if ($ this-> fileSize> $ this-> maxsize) {$ this-> setOPtion ('errornum ', '-2'); returnfalse;} else {returntrue ;}// used to check the file upload type private function checkFileType () {if (in_array (strtolower ($ this-> fileType), $ this-> allowtype) {returntrue;} else {$ this-> setOption (' Errornum',-1); returnfalse ;}// set the name of the uploaded file private function setNewFileName () {if ($ this-> israndname) {$ this-> setOption ('newfilename', $ this-> proRandName ();} else {$ this-> setOption ('newfilename ', $ this-> originName) ;}// set the random file name private function proRandName () {$ fileName = date ("YmdHis "). rand (100,999); return $ fileName. '. '. $ this-> fileType;} private function setOption ($ key, $ val) {$ this-> $ key = $ va L ;}// used to upload a file function uploadFile ($ fileField) {$ return = true; // check the file upload path if (! $ This-> checkFilePath () {$ this-> errorMess = $ this-> getError (); returnfalse ;} $ name = $ _ FILES [$ fileField] ['name']; $ tmp_name = $ _ FILES [$ fileField] ['tmp _ name']; $ size = $ _ FILES [$ fileField] ['size']; $ error = $ _ FILES [$ fileField] ['error']; if (is_Array ($ name )) {$ errors = array (); for ($ I = 0; $ I
SetFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {if (! $ This-> checkFileSize () |! $ This-> checkFileType () {$ errors [] = $ this-> getError (); $ return = false ;}} else {$ error [] = $ this-> getError (); $ return = false;} if (! $ Return) $ this-> setFiles ();} if ($ return) {$ fileNames = array (); for ($ I = 0; $ I
SetFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {$ this-> setNewFileName (); if (! $ This-> copyFile () {$ errors = $ this-> getError (); $ return = false ;} else {$ fileNames [] = $ this-> newFileName ;}}$ this-> newFileName = $ fileNames ;}$ this-> errorMess = $ errors; return $ return ;} else {if ($ this-> setFiles ($ name, $ tmp_name, $ size, $ error) {if ($ this-> checkFileSize () & $ this-> checkFileType () {$ this-> setNewFileName (); if ($ this-> copyFile () {returntrue ;} else {$ return = false ;}} else {$ return = fa Lse ;}} else {$ return = false;} if (! $ Return) $ this-> errorMess = $ this-> getError (); return $ return;} private function copyFile () {if (! $ This-> errorNum) {$ filepath = rtrim ($ this-> filepath ,'/'). '/'; $ filepath. = $ this-> newFileName; if (@ move_uploaded_file ($ this-> tmpFileName, $ filepath) {returntrue;} else {$ this-> setOption ('errornum ', -3); returnfalse ;}} else {returnfalse ;}// set the private function setFiles ($ name = "", $ tmp_name = '', $ size = 0, $ error = 0) {$ this-> setOption ('errornum', $ error); if ($ error) {returnfalse ;} $ this-> setOption ('originname', $ name); $ this-> setOption ('tmpfilename', $ tmp_name); $ arrStr = explode ('. ', $ name); $ this-> setOption ('filetype', strtolower ($ arrStr [count ($ arrStr)-1]); $ this-> setOption ('filesize', $ size); returntrue;} // function getNewFileName () {return $ this-> newFileName ;} // if the upload fails, call this method to view the error report function getErrorMsg () {return $ this-> errorMess ;}}
The above introduces PHP's super easy-to-use file Upload processing class I (37), including some content, and hope to be helpful to friends who are interested in PHP tutorials.