thinkphp realize picture upload function _php skill

Source: Internet
Author: User
Tags file upload php programming

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> &LT;TD width= "83%" ><strong><input type= "text" name= "Gamename"/></strong></td> &LT;/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&gt 
   ; &LT;TD height= > Game address:</td> <td> <input type= "text" name= "GameLink"/></td> </t 
 R> <tr>  &LT;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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.