Thinkphp multi-file Upload

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 description of multifile upload in the Thinkphp manual is clear: to use multiple files, you only need to modify the form

 
 

Change

      
      

Or

      
      

In this way, you can upload multiple files. yes, but there is a problem. Each upload form field corresponds to a database field. Of course, the uploaded files must be stored in the database. This writing method on the manual won't work. The Upload method is written in the model and completed automatically. However, the name of the form field is a name. in this way, only one value is returned for all the three form fields during the upload. So I thought of a method, but I always feel that there will be a better way to solve this problem. The search engine is searched, and there is no effect I want. You can only write it by yourself.

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:

 
 
  1. Image:
  2.  
  3. Video:

Model Code:

 
 
  1. Protected $ info = '';
  2.  
  3. Protected $ _ auto = array (
  4. Array ('image', 'upload', 3, callback), // automatic completion method
  5. Array ('video', 'videoupload', 3, callback), // automatic completion method
  6. ); // Automatically fill in the uploaded image to generate a thumbnail
  7. Protected function upload (){
  8. $ Var = $ _ FILES ['image'] ['name'];
  9. Import ('org. Net. uploadfile ');
  10. $ Upload = new UploadFile ();
  11. $ Upload-> saveRule = time;
  12. $ Upload-> allowExts = array ('jpg ', 'GIF', 'PNG', 'Zip', 'flv ');
  13. $ Upload-> thumb = true;
  14. // Specifies the video path... Only the flv suffix is supported,
  15. $ Upload-> videopath = './Public/upload/Video /';
  16. $ Upload-> savePath = './Public/upload/images /';
  17. $ Upload-> thumbPrefix = '2017 _ 250 _, 150_110 _, 1_156 _';
  18. $ Upload-> thumbMaxWidth = '000000 ';
  19. $ Upload-> thumbMaxHeight = '000000 ';
  20. If (! In_array ('', $ var) |! In_array ('', $ _ FILES ['video'] ['name']) {
  21. If (! $ Upload-> upload ()){
  22. Echo $ upload-> getErrorMsg (); die;
  23. } Else {
  24. $ This-> info = $ upload-> getUploadFileInfo ();
  25. If (! In_array ('', $ var )&&! In_array ('', $ _ FILES ['video'] ['name']) {
  26. Return $ this-> info [1] ['savename'];
  27. } Elseif (! In_array ('', $ var )){
  28. Return $ this-> info [0] ['savename'];
  29. } Else {
  30. Return false;
  31. }
  32.  
  33. }
  34. } Else {
  35. Return flase;
  36. }
  37. }
  38. // Upload a video
  39. Protected function videoupload (){
  40. If (! In_array ('', $ var )&&! In_array ('', $ _ FILES ['video'] ['name']) {
  41. Return $ this-> info [0] ['savename'];
  42. } Elseif (! In_array ('', $ _ FILES ['video'] ['name']) {
  43. Return $ this-> info [1] ['savename'];
  44. } Else {
  45. Return false;
  46. }
  47.  
  48. }

From beginning to end, I felt that there was a very simple solution, but I couldn't find the solution. This is also a stubborn solution. If you have encountered this problem, contact Songlin. Pine forest is eager to solve this problem simply. Baidu, google, and bing have not found any problems.

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.