Php document Upload class

Source: Internet
Author: User
Php file Upload class & lt ;? Php/*** php file Upload class * www.jbxue.com */classUploadFile {/*** supported file Upload types * @ vararray $ allowFileTyp php file Upload class
 AllowFileTypes = explode (',', $ fileTypes);} else {$ this-> allowFileTypes = $ fileTypes;} return ;} /*** Upload file ** @ param string $ the file to be uploaded by fileField, for example, $ _ FILES ['file'] * @ param string $ destFolder, * @ param string $ fileTypes is automatically created. After uploading, the file name is 0. the original file name 1 is used as the file name * @ return int * @ access public */public function upload ($ fileField, $ destFolder = '. /', $ fileNameType = 1) {switch ($ fileField ['error']) {ca Se UPLOAD_ERR_ OK: // The value is 0. no error occurs. the file is uploaded successfully. $ Upload_succeed = true; break; case UPLOAD_ERR_INI_SIZE: // The value is 1. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini. Case UPLOAD_ERR_FORM_SIZE: // The value is 2. the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. $ ErrorMsg = 'file Upload failed! Cause of failure: the file size exceeds the limit! '; $ ErrorCode =-103; $ upload_succeed = false; break; case UPLOAD_ERR_PARTIAL: // value: 3; only part of the file is uploaded. $ ErrorMsg = 'file Upload failed! Cause of failure: only part of the file is uploaded! '; $ ErrorCode =-101; $ upload_succeed = false; break; case UPLOAD_ERR_NO_FILE: // value: 4; no file is uploaded. $ ErrorMsg = 'file Upload failed! Cause of failure: no file is uploaded! '; $ ErrorCode =-102; $ upload_succeed = false; break; case UPLOAD_ERR_NO_TMP_DIR: // The value is 6 and the temporary folder cannot be found. PHP 4.3.10 and PHP 5.0.3 are introduced. $ ErrorMsg = 'file Upload failed! Cause of failure: the temporary folder cannot be found! '; $ ErrorCode =-102; $ upload_succeed = false; break; case UPLOAD_ERR_CANT_WRITE: // if the value is 7, file writing fails. PHP 5.1.0 is introduced. $ ErrorMsg = 'file Upload failed! Cause of failure: file writing failed! '; $ ErrorCode =-102; $ upload_succeed = false; break; default: // Other errors $ errorMsg =' file Upload failed! Cause of failure: others! '; $ ErrorCode =-100; $ upload_succeed = false; break;} if ($ upload_succeed) {if ($ fileField ['size']> $ this-> maxFileSize) {$ errorMsg = 'file Upload failed! Cause of failure: the file size exceeds the limit! '; $ ErrorCode =-103; $ upload_succeed = false;} if ($ upload_succeed) {$ fileExt = FileSystem: fileExt ($ fileField ['name']); if (! In_array (strtolower ($ fileExt), $ this-> allowFileTypes) {$ errorMsg = 'file Upload failed! Cause of failure: the file type is not allowed! '; $ ErrorCode =-104; $ upload_succeed = false ;}} if ($ upload_succeed) {if (! Is_dir ($ destFolder) & $ destFolder! = './' & $ DestFolder! = '.. /') {$ Dirname = ''; $ folders = explode ('/', $ destFolder); foreach ($ folders as $ folder) {$ dirname. = $ folder. '/'; if ($ folder! = ''& $ Folder! = '.' & $ Folder! = '..'&&! Is_dir ($ dirname) {mkdir ($ dirname) ;}} chmod ($ destFolder, 0777);} switch ($ fileNameType) {case 1: $ fileName = date ('ymdhis '); $ dot = '. '; $ fileFullName = $ fileName. $ dot. $ fileExt; $ I = 0; // determine whether there is a duplicate file while (is_file ($ destFolder. $ fileFullName) {$ fileFullName = $ fileName. $ I ++. $ dot. $ fileExt;} break; case 2: $ fileFullName = date ('ymdhis '); $ I = 0; // determine whether there is a duplicate file while (is_file ($ destFolder. $ FileFullName) {$ fileFullName = $ fileFullName. $ I ++;} break; default: $ fileFullName = $ fileField ['name']; break;} // die ($ destFolder. $ fileFullName); $ tempfile = str_replace ('\', '\', $ fileField ['tmp _ name']); // $ files = if (move_uploaded_file ($ tempfile, $ destFolder. $ fileFullName) {return $ fileFullName;} else {$ errorMsg = $ destFolder. $ fileFullName. "File upload failed! Cause of failure: an error occurred while reading and writing permissions to the local file system! "; $ ErrorCode =-105; $ upload_succeed = false ;}} if (! $ Upload_succeed) {throw new Exception ($ errorMsg, $ errorCode );}}

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.