The thinkphp form uploads a file and saves the file path to the database,

Source: Internet
Author: User

The thinkphp form uploads a file and saves the file path to the database,

Upload a single file. The upload effect is as follows:

Create a database upload_img to save the upload path

Create table 'seminar _ upload_img '(
'Id' int (11) not null AUTO_INCREMENT,
'Img _ name' varchar (255) default null comment 'image name ',
'Img _ url' varchar (255) default null comment 'image path ',
'Create _ time' text,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 23 default charset = utf8;
Connect to the database in the Common/Conf. php file 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 file upload 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 (); // instantiate the upload class $ upload-> maxSize = 1*1024*1024; // set the attachment upload size $ upload-> exts = array ('jpg ', 'gif ', 'png ', 'jpeg'); // sets the attachment upload type $ upload-> rootPath = 'public/Uploads /'; // set the root directory for uploading attachments $ upload-> savePath = ''; // set the attachment upload (sub) directory $ upload-> saveName = array ('uniqid ', ''); // rules for saving uploaded files $ upload-> autoSub = true; // automatically saves uploaded files using subdirectories $ upload-> subName = array ('date ', 'ymmd'); // upload a single image Slice $ info = $ upload-> uploadOne ($ _ FILES ['image']); if (! $ Info) {// upload error message $ this-> error ($ upload-> getError ());} else {// obtain the information of the uploaded file after the upload is successful $ 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 ('uploaded successfully ');}}}}}

 

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.