PHP design super easy-to-use file Upload processing class 1 (37), _ PHP Tutorial

Source: Internet
Author: User
PHP designed a super easy-to-use file Upload processing class I (37 ),. PHP is designed with a super easy-to-use file Upload processing class I (37 ),? PhpclassFileUpload {private $ filepath; specifies the path to the file to be uploaded, which is private $ allowtypearray (Class 1 (37) of the super easy-to-use file Upload processing class designed by gif and PHP ),
 $ 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) {case 4: $ str. = "no file is uploaded"; break; case 3: $ str. = "The file is only partially uploaded"; break; case 2: $ str. = "the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form"; break; case 1: $ 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); return false;} if (! File_exists ($ this-> filepath) |! Is_writable ($ this-> filepath) {if (! @ Mkdir ($ this-> filepath, 0755) {$ this-> setOption ('errornum',-4); return false ;}} return true ;} // check the file upload size. private function checkFileSize () {if ($ this-> fileSize> $ this-> maxsize) {$ this-> setOPtion ('errornum ', '-2'); return false;} else {return true ;}// used to check the file upload type private function checkFileType () {if (in_array (strtolower ($ this-> fileType), $ this-> allowtype) {return true;} else {$ this-> setO Ption ('errornum',-1); return false ;}// 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 = $ val;} // function used to upload a file uploadFile ($ fileField) {$ return = true; // check the file upload path if (! $ This-> checkFilePath () {$ this-> errorMess = $ this-> getError (); return false ;} $ 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 () {return true ;} else {$ return = false ;}} else {$ return = f Alse ;}} 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) {return true;} else {$ this-> setOption ('errornum ', -3); return false ;}} else {return false ;}// sets the private function setFiles ($ name = "", $ tmp_name = '', $ size = 0, $ error = 0) {$ this-> setOption ('errornum', $ error); if ($ error) {return false;} $ this-> setOption ('originname', $ name); $ this-> setOption ('tmpfilename', $ tmp_name); $ arrStr = explode ('. ', $ name); $ this-> setOption ('filetype', strtolower ($ arrStr [count ($ arrStr)-1]); $ this-> setOption ('filesize', $ size); return true;} // function getNewFileName () {return $ this-> newFileName ;} // if the upload fails, call this method to view the error report function getErrorMsg () {return $ this-> errorMess ;}}

Round (37 ),? Php class FileUpload {private $ filepath; // specifies the path to save the uploaded file private $ allowtype = array ('GIF ','...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.