An example of file upload method encapsulating ThinkPHP

  • 2021-07-24 10:30:39
  • OfStack

This article describes an example of encapsulation of ThinkPHP a file upload method to share for your reference. The details are as follows:

These days, the upload function of WBlog has been optimized. The improved upload function can realize thumbnail upload, picture upload, generate watermark, file upload, and can upload attachments in the background size (file size), type (file type), watermark (for pictures) settings. In addition, the uploaded files can be classified and saved to the corresponding folders, which is convenient for management.

The following is a brief explanation of the improved upload function.

Thumbnail upload

The previous thumbnail upload function used the upload component of KindEditor, Unless the picture is thumbnailed before uploading, the original picture is uploaded. The improved uploading function can set the size of the thumbnail through the system. No matter how big the original picture you uploaded, the picture with the set size will be generated, and the original picture will be automatically deleted, effectively reducing the accumulation of junk information. Uploaded thumbnails are automatically saved to the Thumb folder

Picture upload

Upload pictures, you can set the size of pictures, upload the type of pictures and add watermarks as needed.

File upload

Such as compressed files, document files, etc., you can add uploaded file types and sizes as needed.

Add Watermark

Add a watermark to the uploaded picture.

I encapsulate the above upload function in the model class AttachModel. class. php, and do it in two ways:


/**     
    * Attachment upload : Upload pictures, thumbnails, files      
    * @param string $model Upload the module      
    * @param bool $type Type of file uploaded : : image (Picture); thumb( Thumbnail ) ; file( Documents )     
    */
            
public function upload($model = null, $type = 'image'){     
       // Import and upload classes      
       import('ORG.NET.UploadFile');     
       $upload = new UploadFile();     
       $upload->saveRule = 'uniqid'; // Set up rules for uploading files      
       $upload->maxSize = C('MAXSIZE')*1024*1024;   // Upload file size          
       $upload->allowExts = explode(',', C('ALLOWEXTS'));// File type      
               
       if ($model){     
           $upload->savePath = './Public/Uploads/'.$model.'/';     
           if (!file_exists($upload->savePath)) {     
             mkdir($upload->savePath);     
           }     
       }else{     
           $upload->savePath = './Public/Uploads/Thumb/';     
           if (!file_exists($upload->savePath)) {     
             mkdir($upload->savePath);     
           }     
       }     
       if (in_array($type,array('image','thumb'))){     
           $upload->thumb = true;     
           $upload->thumbRemoveOrigin  = true;// // Delete the original image      
           $upload->thumbPrefix = 'wb_';         
           $upload->thumbMaxWidth = $type=='thumb' ? C('THUMB_W') :C('IMAGE_W');// Thumbnail width      
           $upload->thumbMaxHeight = $type=='thumb' ? C('THUMB_H') :C('IMAGE_H');// Thumbnail height          
                           
       }     
       if (!$upload->upload()) {     
           return $upload->getErrorMsg();     
       }else{     
           $uploadlist = $upload->getUploadFileInfo();     
           if(C('ISWATER') && $type=='image'){     
             import('ORG.Util.Image');     
             // To m_ Watermarking thumbnails , Image::water(' Original file name ',' Watermark image address ')     
             foreach ($uploadlist as $key => $value){     
               Image::water($value['savepath'] . 'wb_' . $value['savename'], './Public/admin/images/water.png');     
            }     
           }     
       }     
       if (in_array($type,array('image','file'))){     
           return $this->_add($uploadlist,$model);     
       }else{     
           return $uploadlist[0]['savename'];// Returns the thumbnail save name      
       }            
   }     
   /*     
    * The uploaded attachments are integrated into attach The required data is stored in the table and returned to the array      
    * */
   private function _add($uploadlist,$module=''){     
       //$j = count($uploadlist);     
       $v = array();     
       foreach ($uploadlist as $key => $value){     
                   
           $v[$key]['name']        =   $value['name'];     
           $v[$key]['savename']    =   $value['savename'];     
           $v[$key]['savepath']    =   substr($value['savepath'], 2);     
           $v[$key]['size']        =   $value['size'];     
           $v[$key]['userId']      =   $_SESSION[C('USER_AUTH_KEY')];     
           $v[$key]['uploadTime']  =   time();     
           $v[$key]['alt']                 =   $_POST['alt'][$key];     
           $v[$key]['module']          =   $_POST['module'];// Current project path       
           $v[$key]['recordId']    =   $_POST['recordId'];// Current project path       
           $this->add($v[$key]);     
           if($this->thumb){     
                       
               $v[$key]['prefix']      =   $this->thumbPrefix;                   
           }     
           $v[$key]['id'] = M('Attach')->getLastInsID();                 
       }     
       return $v;     
}

I hope this article is helpful to everyone's ThinkPHP framework programming.


Related articles: