thinkphp the form to upload the file and save the file path to the database _php instance

Source: Internet
Author: User

Upload a single file, this article to upload pictures for example, upload effect as shown in the picture

Create the 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 CO Mment ' 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 the database in the 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 File 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)) {//Upload a single image $upload = new \think\upload (); Example Upload class $upload->maxsize = 1*1024*1024//Set attachment upload size $upload->exts = array (' jpg ', ' gif ', ' PNG ', ' jpeg ');//Set attachment upload type $up Load->rootpath = ' public/uploads/'; Set attachment upload root directory $upload->savepath = '; Set attachment upload (sub) directory $upload->savename=array (' uniqid ', ');//Upload file Save rule $upload = true;//automatically use subdirectories to save uploaded files $
Upload->subname = Array (' Date ', ' Ymd ');
Upload a single picture $info = $upload->uploadone ($_files[' image '); if (! $info) {//Upload error message error messages $this->error ($upload->geterror ());} else{//upload successfully 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 succeeded ');} }
}
}

The above is a small set to introduce the Thinkphp form upload file and file path to the database, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.