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

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

View files in upload/index.html

 
  
 
  Upload allowed file types: ' jpg ', ' gif ', ' PNG ', ' JPEG ' image file after

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 single image $upload = new \think\upload ();// Instantiate 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 the attachment upload root directory $upload->savepath = '; Set attachments Upload (sub) directory $upload->savename=array (' uniqid ', ');//Upload file Save rule $upload->autosub = true;//automatically save upload file using subdirectory $ Upload->subname = Array (' Date ', ' Ymd ');//upload single picture $info = $upload->uploadone ($_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 successful ');}}}}

The above is a small section to introduce the Thinkphp form upload files and save the file path to the database, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

  • 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.