Just learned to use the thinkphp framework to simply upload attachments (Pictures, documents, videos and other files)
First you need to understand several functions in Upload.class.php (thinkphp/library/think/upload,class,php) in the TP frame
1:geterror () Get upload error message
2:uploadone () upload a single file
3:upload () Upload Multiple files
4:rootpath upload File Save root Path
The code is as follows
Front desk:
< TD > product picture </td><td> < type = "File" name = "Goods_pic"/> </td>
Controller
//process Upload attachments image//Determine if upload 4: No attachments uploaded if($_files[' Goods_pic '] [' Error ']<4) { //Upload $cfg=Array( ' RootPath ' = './public/uploads/',//Save Root Path ); $upload=New\think\upload ($cfg);//fully qualified name instantiation object
When the Uploadone () method executes successfully, it returns information about the name and path of the attachment (on the server) $file _info=$upload->uploadone ($_files[' Goods_pic ']); //save uploaded attachments to database stitching path//$upload->rootpath Gets the RootPath in the configuration through the __get () method in the parent class upload.class.php $_post[' Goods_big_img ']=$upload->rootpath.$file _info[' Savepath '].$file _info[' Savename ']; }
If you have additional requirements for uploading attachments, refer to the parent class Upload.class.php in the Upload configuration config parameter to select the required parameters, just rewrite it in the Code $cfg=array () above!
The parent-class upload configuration parameters are as follows (can be rewritten according to your own requirements):
Private $config=Array( ' Mimes ' =Array(),//file MIME types allowed for uploading' MaxSize ' = 0,//Upload file size limit (0-No limit)' Exts ' =Array(),//allow file suffixes to be uploaded' Autosub ' =true,//Automatic subdirectory Save file' SubName ' =Array(' Date ', ' y-m-d '),//How subdirectories are created, [0]-function name, [1]-parameter, multiple arguments using an array' RootPath ' = './uploads/',//Save Root Path' Savepath ' = ',//Save Path' Savename ' =Array(' uniqid ', '),//upload file naming rules, [0]-function name, [1]-parameter, multiple parameters using array' Saveext ' = ',//file save suffix, empty use original suffix' Replace ' =false,//exists if the same name overrides' Hash ' =true,//whether to generate a hash code' Callback ' =false,//detects if a file has a callback, if there is an array of returned file information' Driver ' = ',//File Upload driver' Driverconfig ' =Array(),//Upload Drive Configuration);
Implementing attachment uploads using thinkphp