ThinkPHP uploads attachments and thinkphp uploads attachments.

Source: Internet
Author: User

ThinkPHP uploads attachments and thinkphp uploads attachments.

I just learned how to use the ThinkPHP framework to upload attachments (images, documents, videos, and other files)

First, you need to understand several functions in Upload. class. php (ThinkPHP/Library/Think/Upload, class, php) in the tp framework.

1: getError () gets the upload error message

2: uploadOne () Upload a single file

3: upload () upload multiple files

4: rootPath: root path for storing uploaded files

The Code is as follows:

Front-end:

<Td> product images </td> <input type = "file" name = "goods_pic"/> </td>

Controller

// Process uploaded attachment images // determine whether to upload 4: if no attachment is uploaded ($ _ FILES ['goods _ pic '] ['error'] <4) {// upload $ cfg = array ('rootpath' => '. /Public/uploads/', // Save the root path); $ upload = new \ Think \ Upload ($ cfg); // fully qualified name instantiation object // uploadOne () after the method is successfully executed, the attachment (on the server) $ file_info = $ upload-> uploadOne ($ _ FILES ['goods _ pic ']); // Save the uploaded attachment to the database splicing path // $ upload-> rootPath through the parent class upload. class. the _ get () method in php gets the configuration's rootPath $ _ POST ['goods _ big_img '] = $ upload-> rootPath. $ file_info ['savepath']. $ file_info ['savename'];}

If you have other requirements for uploading attachments, refer to the parent class Upload. class. in php, upload and configure the config parameter to select the required parameter. You only need to rewrite it in the Code $ cfg = array () above!

The configuration parameters for parent upload are as follows (you can rewrite them as required ):

Private $ config = array ('mimes '=> array (), // The MiMe type of the file that can be uploaded 'maxsize' => 0, // The size of the uploaded file is limited (0-no limit) 'exts' => array (), // The suffix of the file to be uploaded is 'autosub' => true, // The automatic subdirectory saves the file 'subname' => array ('date', 'Y-m-d'), // The subdirectory creation method, [0]-function name, [1]-parameter. Multiple parameters use the array 'rootpath' => '. /Uploads/', // Save the root path 'savepath' => '', // Save the path 'savename' => array ('uniqid ',''), // Upload File naming rule, [0]-function name, [1]-parameter, multiple parameters use array 'savext' => '', // Save the suffix of the file, if it is null, use the original suffix 'replace '=> false, // whether the same name overwrites 'hash' => true, // whether to generate the hash code 'callback' => false, // check whether a file callback exists. If an array of returned file information is 'driver '=> '', // The File Upload driver 'driverconfig' => array (), // upload driver configuration );

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.