Implementing attachment uploads using thinkphp

Source: Internet
Author: User

Just learned to use the thinkphp framework to simply upload attachments (Pictures, 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 frame

1:geterror () Get upload error message

2:uploadone () upload a single file

3:upload () Upload Multiple files

4:rootpath upload File Save root Path

The code is as follows

Front desk:

< TD > product picture </td><td>    <    type = "File" name = "Goods_pic"/>   </td>

Controller

//process Upload attachments image//Determine if upload 4: No attachments uploaded        if($_files[' Goods_pic '] [' Error ']<4) {                    //Upload          $cfg=Array(            ' RootPath ' = './public/uploads/',//Save Root Path          ); $upload=New\think\upload ($cfg);//fully qualified name instantiation object
When the Uploadone () method executes successfully, it returns information about the name and path of the attachment (on the server) $file _info=$upload->uploadone ($_files[' Goods_pic ']); //save uploaded attachments to database stitching path//$upload->rootpath Gets the RootPath in the configuration through the __get () method in the parent class upload.class.php $_post[' Goods_big_img ']=$upload->rootpath.$file _info[' Savepath '].$file _info[' Savename ']; }

If you have additional requirements for uploading attachments, refer to the parent class Upload.class.php in the Upload configuration config parameter to select the required parameters, just rewrite it in the Code $cfg=array () above!

The parent-class upload configuration parameters are as follows (can be rewritten according to your own requirements):

Private $config=Array(        ' Mimes ' =Array(),//file MIME types allowed for uploading' MaxSize ' = 0,//Upload file size limit (0-No limit)' Exts ' =Array(),//allow file suffixes to be uploaded' Autosub ' =true,//Automatic subdirectory Save file' SubName ' =Array(' Date ', ' y-m-d '),//How subdirectories are created, [0]-function name, [1]-parameter, multiple arguments using an array' RootPath ' = './uploads/',//Save Root Path' Savepath ' = ',//Save Path' Savename ' =Array(' uniqid ', '),//upload file naming rules, [0]-function name, [1]-parameter, multiple parameters using array' Saveext ' = ',//file save suffix, empty use original suffix' Replace ' =false,//exists if the same name overrides' Hash ' =true,//whether to generate a hash code' Callback ' =false,//detects if a file has a callback, if there is an array of returned file information' Driver ' = ',//File Upload driver' Driverconfig ' =Array(),//Upload Drive Configuration);

Implementing attachment uploads using thinkphp

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.