ThinkPHP Use Heart Score Sharing Upload Use of UploadFile

  • 2021-06-28 11:27:53
  • OfStack

The upload class is easy to use. If you are a friend of the first time using the upload function, it is important to note that the form properties in the html submission form should be added with the enctype properties, such as:


 <form method="post" action="upload.php"  enctype="multipart/form-data">
 <input type='file' name='file'>
 <input type='submit' value=' Submit '>
 </form>

The UploadFile class for ThinkPHP is in the file. /ThinkPHP/Extend/Library/ORG/Net/UploadFile.class.php:


        // Introduce UploadFile class 
        import('ORG.Net.UploadFile');
        // instantiation UploadFile class 
        $upload  = new UploadFile();
        // set file size 
        $upload -> maxSize = 3292200;
        // Set file save rules only 1
        $upload->saveRule = 'uniqid';
        // Format Uploaded Files 
        $upload -> allowExts = array('jpg','png','jpeg');
        // Save Path 
        $upload->savePath ='./Public/Uploads/';
        // Settings require thumbnails to be generated, only valid for image files 
        $upload->thumb = true;
        // Set the file prefix to generate thumbnails 
        $upload->thumbPrefix = 'm_';  // Production thumbnails can also be generated as needed 1 One or more, 2 Zhang: 'm_,s_'
        // Set maximum thumbnail width 
        $upload->thumbMaxWidth = '150';//2 Different settings for Zhang: '150,200'
        // Set maximum height of thumbnail 
        $upload->thumbMaxHeight = '200';
        // Delete original 
        $upload->thumbRemoveOrigin = true;
        // Upload failure returned error message 
        if(!$upload->upload()){
           $this->error($upload->getErrorMsg());
        }else{
           $this->success(' Upload Successful ');
           // Get information about uploaded files 
           $inf= $upload->getUploadFileInfo();
        }

The settings make it easy to upload files, while $upload's getUploadFileInfo can get information about the uploaded files.


Related articles: