thinkphp a form to upload a file and save the file path to the database

Source: Internet
Author: User

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

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.