ThinkPHP file upload example tutorial

Source: Internet
Author: User
This article mainly introduces ThinkPHP file Upload implementation method, which is a very common technique in ThinkPHP program development. if you need ThinkPHP, refer to ThinkPHP.

File upload is a common feature in many PHP program projects. This article will share a complete example to implement the ThinkPHP file upload function. The specific method is as follows:

I. action:

The FileAction. class. php page code is as follows:

<? Phpclass FileAction extends Action {function index () {$ file = M ('file'); $ list = $ file-> select (); $ this-> assign ('filelist', $ list); $ this-> display ();} function upload () {// submit the file upload address to him, after the upload is complete, a message is returned so that it can be written to the database if (empty ($ _ FILES) {$ this-> error ('upload file required ');} else {$ a = $ this-> up (); if (isset ($ )) {// custom c method for writing data to the database if ($ this-> c ($ a) {$ this-> success ('uploaded successfully ');} else {$ this-> error ('database writing failed') ;}} else {$ this-error ('upload file File exception. contact the system administrator ') ;}} private function c ($ data) {$ file = M ('file'); $ num = '0 '; for ($ I = 0; $ I <count ($ data)-1; $ I ++) {$ data ['filename'] = $ data [$ I] ['savename']; if ($ file-> data ($ data)-> add ()) {$ num ++ ;}}if ($ num = count ($ data)-1) {return true ;}else {return false ;}} private function up () {// call the import ('@. org. uploadFile '); // Upload class UploadFile. class. copy php to the Lib/Org folder $ upload = new UploadFile (); $ upload-> maxSize = '000000'; // The default value is-1. the upload size is not limited. $ upload-> savePath = '. /Public/Upload/'; // It is recommended that you save $ upload-> saveRule = uniqid in a directory at the same level as the master file or in a subdirectory of the same level; // rules for saving uploaded file names $ upload-> uploadReplace = true; // specifies whether to overwrite $ upload-> allowExts = array ('jpg ', 'jpeg ', 'PNG', 'GIF'); // type of the file to be uploaded $ upload-> allowTypes = array ('image/png ', 'image/jpg ', 'image/jpeg ', 'image/GIF'); // detect mime Types $ upload-> thumb = true; // whether to enable image file thumbnails $ upload -> ThumbMaxWidth = '000000'; $ upload-> thumbMaxHeight = '000000'; $ upload-> thumbPrefix ='s _, m _'; // prefix of the thumbnail file $ upload-> thumbRemoveOrigin = 1; // if a thumbnail is generated, whether to delete the original image if ($ upload-> upload ()) {$ info = $ upload-> getUploadFileInfo (); return $ info;} else {$ this-> error ($ upload-> getErrorMsg ()); // used to obtain the uploaded error message }}?>

II. view template:

The template file index.html code is as follows:

 
  
Thumbnail:
  
Big chart:

I believe the examples described in this article can be used as a reference for ThinkPHP program development.

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.