Thinkphp Multifile upload implementation method,

Source: Internet
Author: User

Thinkphp Multifile upload implementation method,

This article describes how to implement Thinkphp multi-file upload. The specific implementation method is as follows:

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

Copy codeThe Code is as follows: <input type = 'file' name = 'photo'>
Change
Copy codeThe Code is 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 codeThe Code is as follows: <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:

Copy codeThe Code is as follows: image: <input type = "file" name = "image []">
 
Video: <input type = "file" name = "video []">
Model Code:
Copy codeThe Code is as follows: 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 )&&! 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 )&&! 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.
Copy codeThe Code is 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 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.
Copy codeThe Code is as follows: 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.

I hope this article will help you with ThinkPHP framework programming.




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.