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.