Thinkphp multi-file Upload implementation method _ php instance

Source: Internet
Author: User
This article mainly introduces Thinkphp multi-file Upload implementation method, which is a very practical technique. if you need it, you can refer to the example below to describe Thinkphp multi-file Upload implementation method, share it with you for your reference. 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

The code is as follows:


Change

The code is as follows:



  • Or

    The code is as follows:



  • 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:

    Image:

    Video:


    Model Code:

    The 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.

    The code is as follows:




  • 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.

    The 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.