PHP implementation of common file upload class

Source: Internet
Author: User

1<?PHP2 /**3 * Upload File class4 * @param _path: Server file storage path5 * @param _allowtype: Allows uploading of file types and the corresponding MIME6 * @param _file: Uploaded file information7  */8 classupload{9 Ten     Private $_path; One     Private $_allowtype; A     Private $_file; -     /** - * Constructor Function the * @param string: The path where the uploaded file is stored on the server -      */ -     function__construct ($path= ' ' ) -     { +         $this->_path =$path; -         $this->_allowtype =Array( +                 //Images A' BMP ' = ' image/x-ms-bmp ', at' jpg ' = ' image/jpeg ', -' jpeg ' = ' image/jpeg ', -' gif ' = ' image/gif ', -' png ' = ' image/png ', -' tif ' = ' image/tiff ', -' TIFF ' = ' Image/tiff ', in' TGA ' = ' Image/x-targa ', -' PSD ' = ' image/vnd.adobe.photoshop ', to                 //text +' txt ' = ' text/plain ', -' php ' = ' text/x-php ', the' html ' = ' text/html ', *' htm ' = ' text/html ', $' js ' = ' text/javascript ',Panax Notoginseng' CSS ' = ' text/css ', -' rtf ' = ' text/rtf ', the' Rtfd ' = ' text/rtfd ', +' py ' = ' text/x-python ', A' java ' = ' Text/x-java-source ', the' RB ' = ' Text/x-ruby ', +' sh ' = ' text/x-shellscript ', -' PL ' = ' text/x-perl ', $' SQL ' = ' text/x-sql ', $                 //Application -' exe ' = ' application/octet-stream ', -' Doc ' = ' Application/vnd.ms-word ', the' docx ' = ' Application/vnd.ms-word ', -' xls ' = ' application/vnd.ms-excel ',Wuyi' ppt ' = ' application/vnd.ms-powerpoint ', the' pps ' = ' application/vnd.ms-powerpoint ', -' PDF ' = ' application/pdf ', Wu' xml ' = ' application/xml ', -                 //Audio About' mp3 ' = ' audio/mpeg ', $' Mid ' = ' Audio/midi ', -' Ogg ' = ' audio/ogg ', -' mp4a ' = ' audio/mp4 ', -' wav ' = ' audio/wav ', A' wma ' = ' audio/x-ms-wma ', +                 //Video the' avi ' = ' video/x-msvideo ', -' dv ' = ' VIDEO/X-DV ', $' mp4 ' = ' video/mp4 ', the' mpeg ' = ' video/mpeg ', the' mpg ' = ' video/mpeg ', the' mov ' = ' video/quicktime ', the' WM ' = ' video/x-ms-wmv ', -' flv ' = ' video/x-flv ', in' mkv ' = ' Video/x-matroska ' the             ); the     } About     /** the * Upload function the * @param string: The name value of the form element the * @return [type] +      */ -      Public functionUpload$txtName= ' ' ) the     {Bayi         $this->_file =$_files[$txtName]; the         if($this->_file[' ERROR '] = = 0){ the             $fileType=End(Explode(‘.‘,$this->_file[' name '] )); -             $allowType=Array(); -             foreach($this->_allowtype as $item=$value ){ the                 $allowType[] =$item; the             } the             if( !In_array($fileType,$allowType)){ the                  die(' The uploaded file format is not correct! ‘); -}Else{ the                 if(Move_uploaded_file($this-file[' Tmp_name '], ($this->path).$this-file[' Name '])) the                     { the                         Echo"<script>alert (' upload succeeded! ') </script> ";94                     } the                 Else the                     { the                         Echo"<script>alert (' upload failed! '); </script> ";98                     } About             } - 101}Else{102             //not uploaded correctly103             Switch($this-file[' Error ']){104                  Case1: the                      die(' File size exceeds system limit. ‘);106                      Break;107                  Case2:108                      die(' File size exceeds predefined limits. ‘);109                      Break; the                  Case3:111                      die(' file is fully uploaded. ‘); the                      Break;113                  Case4: the                      die(' No files were uploaded. ‘); the                      Break; the                 default:117                      die(' Upload error ');118                      Break;119             } -         }121     }122     //End Upload123}

PHP implements common file upload classes

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.