A complete example of php-encapsulated upload class for a single file (image,
This example describes the php-encapsulated single file (image) Upload class. We will share this with you for your reference. The details are as follows:
<? Php // encapsulate the single file (image) Upload class in php/* // parameter 1: $ file array five attribute values name, type, size, tmp, error // parameter 2: file storage path $ path // parameter 3: File Upload permitted type $ allow array $ allow = array ('image/jpeg ', 'image/jpg ', 'image/png', 'image/gif') // parameter 4: maximum size of the file to be uploaded $ size // return value: return $ imageName file name */header ('content-type: text/html; charset = UTF-8 '); class Upload {// defines an attribute, save the error message public static $ error; // public function uploadFile ($ file, $ size, $ path) {if (! Is_dir ($ path) {$ this-> mkPath ($ path);} $ allow = array ('image/jpeg ', 'image/jpg ', 'image/png ', 'image/gif'); // first, determine whether the file has been uploaded to the temporary directory if (! Is_array ($ file) {Upload: $ error = 'not a valid file'; return false ;} // determine whether the file is successfully uploaded to the temporary directory. switch ($ file ['error']) {case 1: Upload: $ error = 'the uploaded file exceeds php. the value restricted by the upload_max_filesize option in ini. '; Return false; case 2: Upload: $ error =' the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. '; Return false; case 3: Upload: $ error =' only part of the file is uploaded '; return false; case 4: Upload :: $ error = 'file not upload'; return false; case 6: Upload: $ error = 'temporary folder not found '; return false; case 7: Upload :: $ error = 'file write failed'; return false;} // checks whether the file type is an image if (! In_array ($ file ['type'], $ allow) {Upload: $ error = 'not the required file type'; return false ;} // determine whether the file size is within the permitted range. if ($ file ['SIZE']> $ size) {Upload: $ error = 'exceeds the maximum allowed file size '; return false;} // file transfer if (move_uploaded_file ($ file ['tmp _ name'], $ path. '/'. $ this-> getName ($ file) {return $ file ['name'];} else {return Upload ::$ error ;}} // method for creating a directory private function mkPath ($ path) {mkdir ($ path);} // Rename private function getName ($ file) {return time (). $ file ['name'] ;}}