//update in the words, be sure to write that if judgment Public functionUpdate () {$model= M (' link '); $upload=New\think\upload ();//instantiating an upload class $upload->maxsize = 3145728;//set attachment upload size $upload->exts =Array(' jpg ', ' gif ', ' PNG ', ' jpeg ');//set attachment upload type $upload->rootpath = './public '; Public under the root$upload->savepath = '/uploads/';//Set Attachments upload directory $upload->savename = [' uniqid ', ' pic '];//Set Save prefix $upload->subname = [' Date ', ' y/m/d ']; Save As Date//uploading files and returning information $info=$upload-upload (); if($info) {//update, be sure to write this judgment//traversal information Get path and file name foreach($info as $file){ $img= ' public '.$file[' Savepath '].$file[' Savename ']; } //Save the file name in post to make it easy to create $_post[' picname '] =$img; } if($model-Create ()) { $model-Save (); $this->success (' Update succeeded ', U ("Link/select")); }Else{ $this->error (' update failed ', U ("Link/select")); } }
2. Add ....
//Save the added content Public functionInsert () {$model= M (' link '); $upload=New\think\upload ();//instantiating an upload class $upload->maxsize = 3145728;//set attachment upload size $upload->exts =Array(' jpg ', ' gif ', ' PNG ', ' jpeg ');//set attachment upload type $upload->rootpath = './public ';//Root $upload->savepath = '/uploads/';//Set Attachments upload directory $upload->subname = [' Date ', ' y/m/d '];//Create a folder as a date save picture $upload->savename = [' uniqid ', ' pic '];//Set Save prefix $info=$upload->upload ();//upload File//Array (size=1)//' picname ' +//Array (SIZE=9)//' name ' = = String ' meinv002.jpg ' (len GTH=12)//' type ' = = String ' Image/jpeg ' (length=10)//' size ' = = int 557301//' key ' = = = String ' PICN Ame ' (length=7)//' ext ' = = string ' jpg ' (length=3)//' md5 ' = = String ' 6807fdb5a7d40d72374f0049c46fb70d ' (l ENGTH=32)//' sha1 ' = = String ' 8f9b545211b2dfa49957cf3e0e8f5e8cd68a734e ' (length=40)//' savename ' + = string ' Pic55c74c898d07d.jpg ' (length=20)//' Savepath ' = "string '/uploads/2015/08/09/' (length=20//dump ($upload );//echo ' foreach($info as $file){ $img= ' public '.$file[' Savepath '].$file[' Savename ']; } $_post[' picname '] =$img; Give him the name .if($model-Create ()) { $model->addtime = Time(); $model-Add (); $this->success (' Add Success ', U ("Link/select")); }Else{ $this->error (' Commit failed ', U ("Link/select")); } }
Third, the form
<form role= "form" action= ' __url__/insert ' method= ' POST ' enctype= ' Multipart/form-data ' >
class= "Form-group" > <label> Add picture </label> <input type= "file" name= "Picname" > </div>
TP Upload Image