Tangframework Upload class-PHP source code

Source: Internet
Author: User
Tangframework Upload class
Namespace Tang \ Util; use Tang \ Exception \ SystemException; use Tang \ Services \ FileService; use Tang \ Storage \ Drivers \ IStorage; /*** file Upload component * Class Upload * @ package Tang \ Util */class Upload {/*** file suffix * @ var array */private $ allowExts = array ('jpg ', 'jpeg ', 'GIF', 'PNG', 'bmp ');/*** check whether the security type is correct * @ var boolean */private $ allowSafeCheck = true; /*** file suffix * @ var string */private $ ext = '';/*** Upload the maximum size (K is in the unit of 1 M = 1024 K) * @ var int */private $ maxSize = 1024;/*** storage Engine * @ var IStorage */protected $ storage; /*** @ param IStorage $ storage engine * @ param int $ maxSize maximum size * @ param bool $ allowSafeCheck whether security check is allowed * @ param array $ exts indicates the suffix that can be uploaded * /public function _ construct (IStorage $ storage, $ maxSize = 0, $ allowSafeCheck = true, array $ exts = array () {$ this-> setStorage ($ storage)-> setMaxSize ($ maxSize)-> setA LlowSafe ($ allowSafeCheck); if ($ exts) {$ this-> setAllowExts ($ exts );}} /*** set the maximum size of the uploaded data to 0 when it is set to 0 * @ param int $ maxSize * @ return $ this */public function setMaxSize ($ maxSize = 0) {$ maxSize = (int) $ maxSize; $ this-> maxSize = $ maxSize> 0? $ MaxSize: 0; return $ this ;} /*** set the file suffix array that can be uploaded * @ param array $ exts * @ return $ this */public function setAllowExts (array $ exts) {$ this-> allowExts = $ exts; return $ this ;} /*** set whether to allow security type check * @ param boolean $ allowSafeCheck * @ return $ this */public function setAllowSafe ($ allowSafeCheck = true) {$ this-> allowSafeCheck = $ allowSafeCheck; return $ this;}/*** set storage Engine * @ param IStorage $ storage * @ Return $ this */public function setStorage (IStorage $ storage) {$ this-> storage = $ storage; return $ this ;} /*** if the file control contains array information similar to file [], if you need to use your own address. * $ FileName: you need to pass an array to the file to be uploaded * @ param string $ filed the name of the field to be uploaded in the file control * @ param mixed $ if the file name generated by fileName is not filled in, it is the system automatically generate the file address * @ return array * @ throws NotFoundFileUploadException * @ throws UploadErrorException * @ throws UploadSafeCheckException */public function move ($ filed, $ fileName = null) {if (! Isset ($ _ FILES [$ filed]) {throw new NotFoundFileUploadException ($ filed);} $ uploadFile = $ _ FILES [$ filed]; if (is_array ($ uploadFile ['name']) {$ fileStructs = array (); if (! Is_array ($ fileName) {$ fileName = array ();} foreach ($ uploadFile ['name'] as $ key => $ value) {$ fileStructs [$ key] = $ this-> uploadHandle (array ('name' => $ value, 'type' => $ uploadFile ['type'] [$ key], 'tmp _ name' => $ uploadFile ['tmp _ name'] [$ key], 'Error' => $ uploadFile ['error'] [$ key], 'size' => $ uploadFile ['size'] [$ key]), isset ($ fileName [$ key]) & $ fileName [$ key]? $ FileName [$ key]: '');} return $ fileStructs;} else {return $ this-> uploadHandle ($ uploadFile, $ fileName );}} private function uploadHandle ($ uploadFile, $ fileName = '') {if ($ uploadFile ['error']> 0) {throw new UploadErrorException ('file upload error '. $ uploadFile ['error'], null, 50001 + $ uploadFile ['error']);} else if (! Is_uploaded_file ($ uploadFile ['tmp _ name']) {throw new UploadErrorException ('illegal file upload! ', Null, 50010);} // Determine the suffix $ this-> ext = FileService: getService ()-> getExtension ($ uploadFile ['name']); if ($ this-> allowExts &&! In_array ($ this-> ext, $ this-> allowExts) {throw new UploadSafeCheckException ('only allowed to upload the [% s] file! ', Array (implode (', ', $ this-> allowExts), 50001);} if ($ this-> maxSize> 0) {$ maxSize = 1024 * $ this-> maxSize; if ($ uploadFile ['size']> $ maxSize) {throw new UploadErrorException ('upload file size is [% d byte], is greater than the set of [% d byte]! ', Array ($ uploadFile ['size'], $ maxSize), 50000 );}} // security type check if ($ this-> allowExts & in_array ($ this-> ext, array ('jpeg ', 'jpg', 'GIF', 'PNG ', 'bmp '))&&! $ This-> picScan ($ uploadFile ['tmp _ name']) {throw new UploadSafeCheckException ('The uploaded file is not a [% s] file! ', Array ($ this-> ext), 50011 );}! $ FileName & $ fileName = $ this-> createFilePath (); $ fileStruct = $ this-> storage-> putFile ($ uploadFile ['tmp _ name'], $ fileName); $ fileStruct ['name'] = $ uploadFile ['name']; return $ fileStruct ;} /*** check whether the file is an image * @ param string $ fileName * @ return boolean */private function picScan ($ fileName) {$ tempImageSize = @ getimagesize ($ fileName ); list ($ tempWidth, $ tempHeight, $ tempType) = (array) $ tempImageSize; $ tempSize = $ tempWidth * $ tempHeight; if ($ tempSize> 16777216 | $ tempSize <4 | empty ($ tempType) | strpos ($ tempImageSize ['Mime '], 'flash')> 0) {return false;} return true;}/*** build Upload path */private function createFilePath () {return date ('ym/d /'). sha1 (uniqid (mt_rand (), 1 )). '. '. $ this-> ext ;}}

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.