ThinkPHP File Upload example tutorial,
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:
<Html> <body> <volist name = "filelist" id = "vo"> thumbnail: <br/> large image: <br/> </volist> <form action =" _ URL _/upload "method =" post "enctype =" multipart/form-data "> <input type =" file "name =" file [] "/> <br/> <input type = "file" name = "file []"/> <br/> <input type = "file" name = "file []"/> <br/> <input type = "submit" value = "Upload"/> </form> </body>
I believe the examples described in this article can be used as a reference for ThinkPHP program development.