Upload a single file, this article to upload pictures as an example, upload effect
Create database upload_img to save the upload path
CREATE TABLE ' seminar_upload_img ' (
' id ' int (one) not NULL auto_increment,
' Img_name ' varchar (255) DEFAULT NULL COMMENT ' picture name ',
' Img_url ' varchar (255) DEFAULT NULL COMMENT ' picture path ',
' Create_time ' text,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=23 DEFAULT Charset=utf8;
Connect to the database in public profile common/conf.php and configure the address
Return Array ( ' db_type ' = ' mysql ', ' db_host ' = ' 127.0.0.1 ', ' db_name ' = ' seminar ', ' db_ ' USER ' = ' root ', ' db_pwd ' + ' root ', ' Db_port ' =>3306, ' db_prefix ' = ' seminar_ ', ' db_ CHARSET ' = ' utf8 ', ' Show_page_trace ' =>true, /* Address replace * /' tmpl_parse_string ' =>array ( ' _ _upload__ ' =>__root__. ' /public/uploads ', ),);
View files in upload/index.html
<! DOCTYPE html>
Implement upload file in controller UploadController.class.php
Namespace Home\controller;use Think\controller;class Uploadcontroller extends Controller {public Function index () { $img =m (' upload_img '); $sel = $img->order (' create_time desc ')->find (); $this->assign (' data ', $sel); $this->display (); } public Function upload () {$upload _img=m (' upload_img '); if (!empty ($_files)) {//uploads a single image $upload = new \think\upload ();//Instantiate upload class $upload-& Gt;maxsize = 1*1024*1024;//Set attachment upload size $upload->exts = array (' jpg ', ' gif ', ' PNG ', ' jpeg '); Set attachment upload type $upload->rootpath = ' public/uploads/'; Set the attachment upload root directory $upload->savepath = "; Set the attachment upload (sub) directory $upload->savename=array (' uniqid ', ');//Upload file save rule $upload->autosub = tr ue;//automatically uses subdirectories to save uploaded files $upload->subname = Array (' Date ', ' Ymd '); Upload a single picture $info = $uploadUploadone ($_files[' image '); if (! $info) {//Upload error message $this->error ($upload->geterror ()); }else{//Upload successfully uploaded file information $img _url= $info [' Savepath ']. $info [' Savename ']; $data [' Img_url ']= $img _url; $data [' Img_name ']= $info [' Savename ']; $data [' Create_time ']=now_time; $upload _img->create ($data); $result = $upload _img->add (); if (! $result) {$this->error (' Upload failed! ‘); }else{$this->success (' upload success '); } } } }}thinkphp a form to upload a file and save the file path to the database