Thinkphp3.2 upload method usage tutorial

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn TP3.2, but the original upload class has been changed. How to use the new class? Here is a simple tutorial.
When TP3.2 came out, but the original upload class was changed, how should I use the new class? Here is a simple tutorial. // Namespace
Namespace Open \ Controller;
// Load the Controller class
Use Think \ Controller;
// Load the upload class
Use Think \ Upload;

Class FileController extends Controller {

Public function test_upload ($ ftype = 'image '){
If ($ ftype = 'image '){
$ Ftype = array ('jpg ', 'gif', 'png', 'jpeg ');
} Else if ($ ftype = 'file '){
$ Ftype = array ('zip ', 'Doc', 'rar', 'xls ');
}

$ Setting = array (
'Mimes '=> '', // MiMe type of the file to be uploaded
'Maxsize' => 6*1024*1024, // the size limit of the uploaded file (0-no limit)
'Exts' => $ ftype, // file suffix that can be uploaded
'Autosub' => true, // automatically saves files in sub-Directories
'Subname' => array ('date', 'Y-m-d'), // subdirectory creation method, [0]-function name, [1]-parameter, use Arrays for multiple parameters
'Rootpath' => './Uploads/', // Save the root path
'Savepath' => '', // save path
);

/* Call the File Upload Component to upload a file */
// Instantiate the upload class and input the above configuration Array
$ This-> uploader = new Upload ($ setting, 'local ');
$ Info = $ this-> uploader-> upload ($ _ FILES );

// Determine whether the upload is successful
If ($ info ){
//// Obtain the information of the uploaded object
Foreach ($ info as & $ file ){
// Concatenate the upload directory
$ File ['rootpath'] =. ltrim ($ setting ['rootpath'], ".");
// Concatenate the relative path of the file
$ File ['filepath'] = $ file ['rootpath']. $ file ['savepath']. $ file ['savename'];
}
// Output the result here. The key name of the relative path is $ info ['upload'] ['filepath']
Dump ($ info ['upload']);
Exit ();
} Else {
// Output error message
Exit ($ this-> uploader-> getError ());
}
}
}
Now, the above Code has completed the basic upload function. If you want to test the function, you can directly submit the form to this method to see the result.

Example: This file is stored in the Open Module (the original group.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.