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 Replacement */ ' tmpl_parse_string ' =array( ' __upload__ ' =>__root__. ' /public/uploads ', ),);
View files in upload/index.html
<!DOCTYPE HTML><HTML> <Head> <title></title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <styletype= "Text/css">#img{Height:22px;Border:#000 2px Solid}#button{Height:30px;width:100px;} </style> </Head> <Body> <Div><notemplyname= "Data"> <imgsrc= "__upload__/{$data [' Img_url ']}"width= "$"Height= "+"/> </notemply></Div> <Divclass= "Result" >Upload allowed file types: ' jpg ', ' gif ', ' PNG ', ' JPEG ' image file after</Div><BR> <formAction= "{: U (' Upload/upload ')}"Method= "POST"enctype= "Multipart/form-data"> <inputtype= "File"name= "image"/> <inputtype= "Submit"value= "Upload"ID= "button"> </form> </Body></HTML>
Implement upload file in controller UploadController.class.php
namespace Home\controller; UseThink\controller;classUploadcontrollerextendsController { Public functionindex () {$img=m (' upload_img '); $sel=$img->order (' create_time desc ')find (); $this->assign (' Data ',$sel); $this-display (); } Public functionupload () {$upload _img=m (' upload_img '); if(!Empty($_files)){ //uploading a single image $upload=New\think\upload ();//instantiating an upload class $upload->maxsize = 1*1024*1024;//set attachment upload size $upload->exts =Array(' jpg ', ' gif ', ' PNG ', ' jpeg ');//set attachment upload type $upload->rootpath = ' public/uploads/';//Set attachments upload root directory $upload->savepath = ";//Set attachments Upload (sub) directory $upload->savename=Array(' uniqid ', ');//save rules for uploading files $upload->autosub =true;//automatically save uploaded files using subdirectories $upload->subname =Array(' Date ', ' YMD '); //upload a single picture $info=$upload->uploadone ($_files[' Image ']); if(!$info) {//Upload error message $this->error ($upload-GetError ()); }Else{//upload successfully get upload 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 successful '); } } } }}
thinkphp a form to upload a file and save the file path to the database