A complete example of php-encapsulated upload class for a single file (image,

Source: Internet
Author: User

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'] ;}}

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.