This article is mainly for you to introduce the thinkphp implementation of the attachment upload function, pictures, documents, videos and other documents to upload, with a certain reference value, interested in small partners can refer to
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 images </td><td> <input type= "file" Name= "Goods_pic"/></td>
Controller
Process upload attachment image //Determine if upload 4: no upload attachment 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 The //uploadone () method returns information about the name and path of the attachment (on the server) after successful execution $file _info= $upload->uploadone ($_files[' Goods_pic ']); Save the uploaded attachment to the database stitching path //$upload->rootpath get the RootPath $_post[' goods in the configuration through the __get () method in the parent class upload.class.php _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 (),//Allow upload of file MIME type ' maxSize ' = + 0,//upload file size limit (0-No limit) ) ' exts ' = = Array (),//allow upload of file suffix ' autosub ' = true,//Auto subdirectory save file ' subname ' = = array (' Date ', ' y-m-d '),//subdirectory creation mode, [0]-function name, [1]-parameter, multiple arguments using 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,//existence of the same name overwrite ' hash ' = True,//whether to generate hash code ' callback ' and False,//to detect if a file exists callback if there is a return file information array ' driver ' = ',// File Upload drive ' driverconfig ' = + Array (),//upload drive configuration);