About the picture upload, the realization method many, for example can use jquery, of course, our thinkphp built-in picture upload function, we can use directly, use the following method:
First, the form is written, and we create the following form:
<form action= "__url__/addchk" method= "post" enctype= "Multipart/form-data" > <table "1" cellpadding= "2" width= "99%" align= "center" border= "0" > <tbody> <tr> <th align= "left" colspan=
"2" height= > Add new Game </th> </tr> <tr> <td width= "17%" height= "the" > Game name </td> <TD width= "83%" ><strong><input type= "text" name= "Gamename"/></strong></td> </TR&G
T
<tr> <td height= > Game Pictures </td> <td ><input type= "file" Name= "Gameimg"/></td> </tr> <tr> <td width= "17%" height= "about" > Game profile <span></span></td> <td WI Dth= "83%" ><textarea name= "Gameinfo" cols= "" rows= "5" ></textarea></td> </tr> <tr>
; <TD height= > Game address:</td> <td> <input type= "text" name= "GameLink"/></td> </t
R> <tr> <TD height= "colspan=" 2 "><input type=" Submit "value=" Add "/> <input type=" reset "value=" re-fill "/&G t;</td> </tr> </tbody> </table> </form>
There is a very important place to be sure that it is important to add the enctype= "Multipart/form-data" to the form item, because this is the type that allows you to upload, if not he, can't upload, about enctype several other types , I will explain in later articles. Here we create a new PHP file AddAction.class.php, the code is as follows;
Class Addaction extends action{/** * Description: thinkphp file upload Test function * Version: 1.0/Public Function Addchk () {//import picture upload class
Import ("ORG.Net.UploadFile");
Instantiate the Upload class $upload = new UploadFile ();
$upload->maxsize = 3145728;
Set file upload type $upload->allowexts = array (' jpg ', ' gif ', ' PNG ', ' jpeg '); Set File upload location $upload->savepath = "./public/uploads/";//Here is a description, because thinkphp is a portal file, so here's./public is the public folder under the root directory of the Web site/
/Set File Upload name (according to the time) $upload->saverule = "Times";
if (! $upload->upload ()) {$this->error ($upload->geterrormsg ());
}else{//Upload success, get upload information $info = $upload->getuploadfileinfo ();
//Save form data, including uploaded pictures $game = M ("game");
$game->create ();
$savename = $info [0][' Savename '];
$savepath = $info [0][' Savepath '];
$aa = $savepath. $savename;
Dump ($AA);
$imgurl = "http://demo.dutycode.com/Public/Uploads/". $savename//Here is the URL of the set file note use. Not +//dump ($imgurl); $data [' gamename '] = $_post[' Gamename '];
$data [' gameimg '] = $imgurl;
$data [' gameinfo '] = $_post[' gameinfo '];
$data [' gamelink '] = $_post[' GameLink '];
$data [' publishtime '] = Date ("y-m-d h:i:s"); $res = $game->add ($data)//write database if ($res) {$this->redirect ("Addgame", "", 2, "add success!")
Two seconds to jump back "); }else{$this->redirect ("Addgame", "", 2, "Failed!
Two seconds to jump back ");
}
}
Here, the first is that the connector in PHP is a dot (.) is not +, which is commonly used in many Java users to connect two strings, so this is easy to confuse.
The above is for thinkphp picture upload function to implement the introduction of code to help you better learn to master PHP programming.