thinkphp Multi-File Upload implementation Method _php example

Source: Internet
Author: User
Tags file upload

The example of this article tells the thinkphp upload implementation method, share to everyone for reference. The implementation methods are as follows:

thinkphp Manual for multiple file upload description is clear: If you need to use more than one file, only need to modify the form, the

Copy Code code as follows:
<input type= ' file ' name= ' photo ' >

To
Copy Code code as follows:
<li><input type= ' file ' name= ' photo1 ' ></li>
<li><input type= ' file ' name= ' Photo2 ' ></li>
<li><input type= ' file ' name= ' Photo3 ' ></li>

Or

Copy Code code as follows:
<li><input type= ' file ' name= ' photo[] ' ></li>
<li><input type= ' file ' name= ' photo[] ' ></li>
<li><input type= ' file ' name= ' photo[] ' ></li>

For the moment, upload your own form field for two, one upload picture, one upload video. Field name is image, video.
The HTML code is as follows

Copy Code code as follows:
Photo: <input type= "file" Name= "image[]" >

Video: <input type= "file" Name= "video[]" >

Model Code:
Copy Code code as follows:
protected $info = ';

Protected $_auto = Array (
Array (' image ', ' upload ', 3,callback),//Automatic completion method
Array (' video ', ' Videoupload ', 3,callback),//Automatic completion method
)//Automatically populate upload image 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 ... Only FLV suffixes are supported,
$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 ();d ie;
}else{
$this->info = $upload->getuploadfileinfo ();
if (!in_array (', $var) &&!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) &&!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 multiple file upload, first look at the HTML code
Copy Code code as follows:
<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 special variable can store a number of indefinite long content, so we can customize the multiple file upload box, then in the PHP processing how we want to operate, see the example below.
Copy Code code as follows:
Protected $_auto = Array (
Array (' image ', ' upload ', 3,callback),//Automatic completion method
Array (' video ', ' Videoupload ', 3,callback),//Automatic completion method
)//Automatically populate upload image to generate thumbnails

This is to tell thinkphp is an array variable, does not need to be like the original eco-PHP to judge all the time to enumerate the group leader and upload the code, because the thinkphp has been done.

I hope this article will be helpful to everyone's thinkphp framework program design.

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.