Very classic PHP file Upload class sharing, _ PHP Tutorial

Source: Internet
Author: User
Tags learn php programming php file upload
Very classic php file Upload class sharing ,. The classic php file Upload class is shared. file upload is a common feature in project development, but the file upload process is cumbersome, as long as files are uploaded, you need to compile a very classic php file Upload class for sharing,

File UploadIt is a common function in project development, but the file upload process is cumbersome. as long as there is a file upload, you need to write these complex code. In order to reduce the difficulty of writing functions during each development and reduce the development time, we usually encapsulate the code that is repeatedly used into a class.

<? Php/** + Upload * file Upload class + ------------------------------------------------------------------------------- * @ author Administrator + Upload */class FileUpload {private $ filepath; // save path private $ allowtype = array ('GIF', 'jpg ', 'jpeg', 'PNG ', 'txt'); private $ maxsize = 10 00000; // maximum allowed Upload size private $ israndname = true; // random private $ orginame; // original file name private $ tmpname; // temporary file name private $ newname; // new file name private $ filetype; // file type private $ filesize; // file size private $ errornum = ''; // error code private $ errormsg; // error message/** + response * constructor + -------------------------------------------------------------------------- ---- * @ Param string $ savepath storage path * @ param string $ allowtype allowed type * @ param string $ maxsize allowed size * + allowed */function _ construct ($ option = array ()) {foreach ($ option as $ key => $ value) {if (! In_array ($ key, get_class_vars (get_class ($ this) {continue;} $ this-> setOption ($ key, $ value) ;}} function uploadfile ($ field) {$ return = true; if (! $ This-> CheckPath () {$ this-> errormsg = $ this-> geterrorNum (); return false ;} $ name = $ _ FILES [$ field] ['name']; $ tmpname = $ _ FILES [$ field] ['tmp _ name']; $ filesize = $ _ FILES [$ field] ['size']; $ error = $ _ FILES [$ field] ['error']; if (is_array ($ name )) {$ errors = array (); for ($ I = 0; $ I
 
  
GetFile ($ name [$ I], $ tmpname [$ I], $ filesize [$ I], $ errors [$ I]) {if (! $ This-> CheckSize ()&&! $ This-> CheckType () {$ errors = $ this-> getErrorNum (); return false ;}} else {$ errors = $ this-> getErrorNum (); return false;} if (! $ Return) {$ this-> getFile () ;}} if ($ return) {$ fileNames = array (); for ($ I = 0; $ I
  
   
GetFile ($ name [$ I], $ tmpname [$ I], $ filesize [$ I], $ filesize [$ I]) {$ this-> SetFileName (); if (! $ This-> MoveFile () {$ errors [] = $ this-> getErrorNum (); $ return = false ;} else {$ fileNames [] = $ this-> getNewName () ;}}$ this-> newname = $ fileNames;} $ this-> errormsg = $ errors; return $ return;} else {if ($ this-> getFile ($ name, $ tmpname, filesize, $ error) {if (! $ This-> CheckSize () {return false;} if (! $ This-> CheckType () {return false;} $ this-> SetFileName (); if ($ this-> MoveFile () {return true ;}} else {return false;} if (! $ Return) {$ this-> setOption ('errornum', 0); $ this-> errormsg = $ this-> geterrorNum ();} return $ return ;}} /** + functions * set the class property value function + functions * @ param mix $ key * @ param mix $ value */private function setOption ($ key, $ value) {$ key = strtolower ($ key); $ this-> $ key = $ value;}/** + --- Required * get the file variable parameter function + required * @ param string $ name * @ param string $ tmp_name * @ param number $ size * @ param number $ error */private function getFile ($ name, $ tmpname, $ filetype, $ filesize, $ error = 0) {$ this-> setOption ('tmpname', $ TmpName); $ this-> setOption ('orginame ', $ n Ame); $ arrstr = explode ('. ', $ name); $ this-> setOption ('filetype', $ arrstr [count ($ arrstr)-1]); $ this-> setOption ('filesize ', $ filesize); return true;}/** + ------------------------------------------------------------------------- * Check the upload path function + callback * @ return boolean */private function CheckPath () {if (empty ($ this-> filepath) {$ this-> s EtOption ('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 ;} private function Is_Http_Post () {if (! Is_uploaded_file ($ this-> tmpname) {$ this-> setOption ('errornum',-6); return false;} else {return true ;}} /** + -------------------------------------------------------------------- * check the file size function + -------------------------------------------------------------------- * @ return boolean */private function CheckSize () {if ($ this-> filesize> $ this-> maxsize) {$ this-> setOption ('errornum',-2); return false;} el Se {return true;}/** + ----------------------------------------------------------------- * Check File type functions + functions * @ return boolean */private function CheckType () {if (in_array ($ this-> filetype, $ this-> allowtype) {return true;} else {$ this-> setOption ('errornum',-1 ); return false ;}} private function SetFileName () {if ($ this-> israndname) {$ th Is-> setOption ('newname', $ this-> RandName ();} else {$ this-> setOption ('newname', $ this-> orginame );}} /** + response * get new file name + ------------------------------------------------------------------ */public function getNewName () {return $ this-> newname;} private function RandName () {$ rule = date ("YmdHis "). rand (0,999); return $ rule. '. '. $ this-> file Type;} private function MoveFile () {if ($ this-> errornum) {$ filepath = rtrim ($ this-> filaepath ,'/'). '/'; $ filepath. = $ this-> newname; if (@ move_uploaded_file ($ this-> tmpname, $ filepath) {return true ;} else {$ this-> errormsg = $ this-> setOption ('errornum',-3) ;}} else {return false ;}} /** + ---------------------------------------------------------------- * error message function + ---------------------------------------- ------------------------ * @ Return string */function getErrorNum () {$ erstr = "an error occurred while uploading the file {$ this-> orginame}"; switch ($ this-> errornum) {case 4: $ erstr. = "no file is uploaded"; break; case 3: $ erstr. = "The file is only partially uploaded"; break; case 2: $ erstr. = "the uploaded file exceeds the value specified by the HTML form MAX_FILE_SIZE"; break; case 1: $ erstr. = "The file uploaded exceeds php. the value of upload_max_filesize in The ini configuration file "; break; case 0: $ erstr =" Upload {$ this-> orginame} succeeded "; break; case-1: $ erstr = "unsupported type"; bre Ak; case-2: $ erstr. = "The file is too large to exceed {$ this-> maxsize} bytes"; break; case-3: $ erstr. = "Upload failed"; break; case-4: $ erstr = "failed to create the upload Directory. please specify the upload Directory Again"; break; case-5: $ erstr = "unspecified Upload path"; break; case-6: $ erstr = "invalid operation"; break; default: $ erstr. = "Unknown error";} return $ erstr ;}}?>
  
 

The above is all the content of this article. I hope it will help you learn php programming.

Upload, file upload is a common feature in project development, but the file upload process is cumbersome, as long as there is a file upload, it needs to be edited...

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.