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