Implementation method of Thinkphp multi file uploading

  • 2021-07-22 09:21:42
  • OfStack

This article example narrates the Thinkphp multi-file upload realization method, shares to everybody for everybody reference. The specific implementation method is as follows:

The Thinkphp manual describes multiple file uploads very clearly: If you need to use multiple files, you only need to modify the form and put

<input type='file' name='photo'>

Replace with
<li><input type='file' name='photo1'></li>
<li><input type='file' name='photo2'></li>
<li><input type='file' name='photo3'></li>

Or

<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>

For the time being, there are two upload form fields, one for uploading pictures and one for uploading videos. The fields are named image, video.
The html code is as follows

 Picture :<input type="file" name="image[]">
 
Video :<input type="file" name="video[]">

model code:
protected $info= '';
 
protected $_auto = array(
array('image','upload',3,callback),// Automatic completion method
array('video','videoupload',3,callback), // Automatic completion method
);// Automatically fill uploaded pictures to generate thumbnails
protected function upload(){
$var = $_FILES['image']['name'];
import('ORG.Net.UploadFile');
$upload = new UploadFile();
$upload->saveRule  = time;
$upload->allowExts  = array('jpg', 'gif', 'png', 'zip','flv');
$upload->thumb = true;
// Video path. . . Support only flv Suffix,
$upload->videopath = './Public/upload/Video/';
$upload->savePath =  './Public/upload/images/';
$upload->thumbPrefix = '250_115_,150_110_,213_156_';
$upload->thumbMaxWidth='250,150,213';
$upload->thumbMaxHeight='115,110,156';
if(!in_array('',$var) || !in_array('',$_FILES['video']['name'])){
if(!$upload->upload()) {
echo $upload->getErrorMsg();die;
}else{
$this->info =  $upload->getUploadFileInfo();
if(!in_array('',$var) &amp;&amp; !in_array('',$_FILES['video']['name'])){
return $this->info[1]['savename'];
}elseif(!in_array('',$var)){
return $this->info[0]['savename'];
}else{
return false;
}
 
}
}else{
return flase;
}
}
// Upload video
protected function videoupload(){
if(!in_array('',$var) &amp;&amp; !in_array('',$_FILES['video']['name'])){
return $this->info[0]['savename'];
}elseif(!in_array('',$_FILES['video']['name'])){
return $this->info[1]['savename'];
}else{
return false;
}
 
}

At the end of the article, I will analyze the principle of uploading multiple files under 1. Let's take a look at html code first
<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>
<li><input type='file' name='photo[]'></li>

This is to define the form variable as an array. In php, the array special variable can store multiple indefinite contents, so we can customize the multi-file upload box. Then how do we operate when processing php? Look at the example below.
protected $_auto = array(
array('image','upload',3,callback),// Automatic completion method
array('video','videoupload',3,callback), // Automatic completion method
);// Automatically fill uploaded pictures to generate thumbnails

This is to tell thinkphp is an array variable, and it is not necessary to judge the traversal array length and upload the code one by one like the original php, because thinkphp has been done.

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


Related articles: