PHP design super easy to use File upload processing class one (PNS), _php tutorial

Source: Internet
Author: User

PHP Design Super Useful file upload processing class One (37),


 PhpclassFileUpload {Private$filepath;//specify the path where the upload file is saved        Private$allowtype =array ('gif','jpg','PNG','JPEG');//the type of file that is allowed to upload        Private$maxsize =1000000;//maximum length of uploaded file 1M        Private$israndname =true;//whether to randomly rename, true false not random, use the original file name        Private$originName;//source file name        Private$tmpFileName;//Temporary file name        Private$fileType;//File Type        Private$fileSize;//File Size        Private$newFileName;//New file name        Private$errorNum =0;//Error Number        Private$errorMess ="";//used to provide error reporting//used to initially enable uploading of files//1. Specify the upload path, 2, allowable type, 3, limit size, 4, whether to use random file name//so that the user can not pass the parameters by location, the following parameter to the value does not have the first several parameters also provide valuesfunction __construct ($options =Array ()) {            foreach($options as$key =$val) {$key=Strtolower ($key); //See if the index of the array in the user parameter is the same as the member property name                if(!in_array ($key, Get_class_vars (Get_class ($ This)))){                    Continue; }                $ This-setOption ($key, $val); }                         }            Privatefunction GetError () {$str="error uploading file {$this->originname}:"; Switch($ This-errornum) {                 Case 4: $str. ="no files were uploaded"; Break;  Case 3: $str. ="files are 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 upload file exceeds the value of the upload_max_filesize option in PHP.ini"; Break;  Case-1: $str. ="type of final charge"; Break;  Case-2: $str. ="file is too large, upload file cannot exceed {$this->maxsize} bytes"; Break;  Case-3: $str. ="Upload failed"; Break;  Case-4: $str. ="failed to set up directory to upload files, please re-specify upload directory"; Break;  Case-5: $str. ="You must specify the path of the upload file"; Break; default: $str. ="Last known error"; }            return$str.'
'; } //used to check file upload path Privatefunction 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; } //to check the size of the file upload Privatefunction Checkfilesize () {if($ This->filesize > $ This-maxsize) { $ This->setoption ('ErrorNum','-2'); return false; }Else{ return true; } } //used to check file upload type Privatefunction Checkfiletype () {if(In_array (Strtolower ($ This->filetype), $ This-Allowtype)) { return true; }Else{ $ This->setoption ('ErrorNum', -1); return false; } } //set the file name after uploading Privatefunction Setnewfilename () {if($ This-israndname) { $ This->setoption ('NewFileName', $ This-prorandname ()); } Else { $ This->setoption ('NewFileName', $ This-originname); } } //set random file names Privatefunction Prorandname () {$fileName=date ("Ymdhis"). Rand ( -,999); return$fileName.'.'.$ This-FileType; } Privatefunction SetOption ($key, $val) {$ This$key =$val; } //used to upload a filefunction UploadFile ($fileField) {$return=true; //Check 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 ){ if($ This- 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 ){ if($ This- 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= false ; } } 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 ; } } // setting up and $_files related content Privatefunction 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 ; } // used to get the file name after uploading function Getnewfilename () { return$ This- NewFileName; } // If the upload fails, call this method to view the error report function geterrormsg () { return$ This- errormess; } }

http://www.bkjia.com/PHPjc/1008857.html www.bkjia.com true http://www.bkjia.com/PHPjc/1008857.html techarticle PHP Design super easy to use File upload processing class one (PNS), PHP class FileUpload {private $filepath;//Specify the path to save the upload 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.