ThinkPHP File Upload Example Tutorial

  • 2021-07-16 01:53:31
  • OfStack

File upload is a common function in many PHP program projects. Today, this article will share a complete example to realize the function of ThinkPHP file upload. The specific methods are as follows:

1. action Part:

The code for the FileAction. class. php page is as follows:


<?php
class FileAction extends Action{
  function index(){
    $file=M('file');
    $list=$file->select();
    $this->assign('filelist',$list);
    $this->display();  
  }  
  function upload(){
    // Submit the file upload address to him and return after the upload is completed 1 Messages to be written to the database   
    if(empty($_FILES)){
      $this->error(' You must choose to upload files ');  
    }else{
      $a=$this->up();
      if(isset($a)){
        // Custom to write to database c Method 
        if($this->c($a)){
          $this->success(' Upload succeeded ');  
        }
        else{
          $this->error(' Failed to write to database ');  
        }
      }else{
        $this-error(' Unexpected file upload, please contact your system administrator ');  
      }
    }
  }
  private function c($data){
    $file=M('file');
    $num  =  '0';
    for($i = 0; $i < count($data)-1; $i++) {
      $data['filename']=$data[$i]['savename'];      
      if( $file->data($data)->add())
      {
        $num++;
      }
    }
    if($num==count($data)-1)
    {
      return true;  
    }else
    {
      return false;
    }
  }
  private function up(){
    // Complete and thinkphp Related, the call of the file upload class   
    import('@.Org.UploadFile');// The class will be uploaded UploadFile.class.php Copy to Lib/Org Under the folder 
    $upload=new UploadFile();
    $upload->maxSize='1000000';// Default to -1 Unlimited upload size 
    $upload->savePath='./Public/Upload/';// It is recommended to save the path to the same directory as the main file or a subdirectory of the same directory   
    $upload->saveRule=uniqid;// File name saving rules for uploaded files 
    $upload->uploadReplace=true;// Do you want to overwrite if there is a file with the same name 
    $upload->allowExts=array('jpg','jpeg','png','gif');// File types allowed to upload 
    $upload->allowTypes=array('image/png','image/jpg','image/jpeg','image/gif');// Detection mime Type 
    $upload->thumb=true;// Turn on picture file thumbnail 
    $upload->thumbMaxWidth='300,500';
    $upload->thumbMaxHeight='200,400';
    $upload->thumbPrefix='s_,m_';// Thumbnail file prefix 
    $upload->thumbRemoveOrigin=1;// If thumbnails are generated, do you want to delete the original image 
    
    if($upload->upload()){
      $info=$upload->getUploadFileInfo();
      return $info;
    }else{
      $this->error($upload->getErrorMsg());// That is specifically used to get uploaded error messages   
    }  
  }
}
?>

2. view template section:

The template file index. html code is as follows:


<html>
<body>
<volist name="filelist" id="vo">
  Small picture: <img src="__PUBLIC__/upload/s_{$vo['filename']}" /><br />
  Big picture: <img src="__PUBLIC__/upload/m_{$vo['filename']}" /><br />
</volist>
<form action="__URL__/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="file[]" /><br />
  <input type="file" name="file[]" /><br />
  <input type="file" name="file[]" /><br />
  <input type="submit" value=" Upload " />
</form>

</body>
</html>

I believe that the examples described in this paper can play a reference role in the development of ThinkPHP program.


Related articles: