Code for multifile Upload using the Thinkphp framework

Source: Internet
Author: User

The description of multifile upload in the Thinkphp manual is clear: to use multiple files, you only need to modify the form

The code is as follows: Copy code

<Input type = 'file' name = 'photo'>
Change

<Li> <input type = 'file' name = 'photo1'> </li>
<Li> <input type = 'file' name = 'photo2'> </li>
<Li> <input type = 'file' name = 'photo3'> </li>
</Ol>
Or

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


Currently, you have two Upload form fields, one for uploading images and one for uploading videos. The field names are image and video.

The html code is as follows:

The code is as follows: Copy code

Figure: <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 in the uploaded image to generate a thumbnail
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;
// Specifies the video path... Only the flv suffix is supported,
$ Upload-> videopath = './Public/upload/Video /';
$ Upload-> savePath = './Public/upload/images /';
$ Upload-> thumbPrefix = '2017 _ 250 _, 150_110 _, 1_156 _';
$ Upload-> thumbMaxWidth = '000000 ';
$ Upload-> thumbMaxHeight = '000000 ';
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 ;! 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 a video
Protected function videoupload (){
If (! In_array ('', $ var) & 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 this article, I will analyze the multifile Upload principle. Let's take a look at the html code.

<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 content, so we can customize the multifile Upload box, the following is an example of how to operate php.

Protected $ _ auto = array (
Array ('image', 'upload', 3, callback), // automatic completion method
Array ('video', 'videoupload', 3, callback), // automatic completion method
); // Automatically fill in the uploaded image to generate a thumbnail

This tells thinkphp that it is an array variable and does not need to determine the length of the traversal array and then upload the code one by one, because thinkphp is ready.

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.