This article mainly introduces a file upload method encapsulated in ThinkPHP. It describes in detail the implementation of the file upload class and its specific functions in the form of an instance, which is very practical. For more information, see
This article mainly introduces a file upload method encapsulated in ThinkPHP. It describes in detail the implementation of the file upload class and its specific functions in the form of an instance, which is very practical. For more information, see
This example describes how to upload ThinkPHP files. The details are as follows:
This time, the WBlog upload function was optimized. The improved upload function allows you to upload thumbnails, upload images, generate watermarks, and upload files. You can also size (file size), type (file type), and ), watermark (for images. In addition, you can classify uploaded files and save them to corresponding folders for convenient management.
The following is a brief description of the improved upload function.
Thumbnail upload
The previous thumbnail upload function uses the KindEditor upload component. The source image is uploaded unless the image is scaled down before the upload, the improved upload function allows you to set the size of the thumbnail. No matter how large the source image you upload is, an image of the specified size is generated and the source image is automatically deleted, effectively reduces the accumulation of junk information. The uploaded thumbnail is automatically saved to the Thumb folder.
Upload images
You can set the size, type, and watermark of the uploaded image as needed.
File Upload
Such as compressed files and document files, you can add the upload file type and size as needed.
Add watermark
Add a watermark to the uploaded image.
The upload function above encapsulates it in the model class AttachModel. class. php and uses two methods:
The Code is as follows:
/**
* Attachment upload: uploads images, thumbnails, and files.
* @ Param string $ module in which the model is uploaded
* @ Param bool $ type: image (image), thumb (thumbnail), and file (file)
*/
Public function upload ($ model = null, $ type = 'image '){
// Import upload class
Import ('org. NET. uploadfile ');
$ Upload = new UploadFile ();
$ Upload-> saveRule = 'uniqid'; // sets the upload File rules.
$ Upload-> maxSize = C ('maxsize') * 1024*1024; // size of the uploaded file
$ Upload-> allowExts = explode (',', C ('allowexts '); // file type
If ($ model ){
$ Upload-> savePath = './Public/Uploads/'. $ model .'/';
If (! File_exists ($ upload-> savePath )){
Mkdir ($ upload-> savePath );
}
} Else {
$ Upload-> savePath = './Public/Uploads/Thumb /';
If (! File_exists ($ upload-> savePath )){
Mkdir ($ upload-> savePath );
}
}
If (in_array ($ type, array ('image', 'thumb '))){
$ Upload-> thumb = true;
$ Upload-> thumbRemoveOrigin = true; // Delete the source Image
$ Upload-> thumbPrefix = 'wb _';
$ Upload-> thumbMaxWidth = $ type = 'thumb '? C ('thumb _ W'): C ('image _ W'); // The width of the thumbnail.
$ Upload-> thumbMaxHeight = $ type = 'thumb '? C ('thumb _ H'): C ('image _ H'); // The height of the thumbnail.
}
If (! $ Upload-> upload ()){
Return $ upload-> getErrorMsg ();
} Else {
$ Uploadlist = $ upload-> getUploadFileInfo ();
If (C ('iswater') & $ type = 'image '){
Import ('org. Util. image ');
// Add a watermark to the m _ thumbnail. Image: water ('original filename ', 'watermark Image address ')
Foreach ($ uploadlist as $ key => $ value ){
Image: water ($ value ['savepath']. 'wb _ '. $ value ['savename'],'./Public/admin/images/water.png ');
}
}
}
If (in_array ($ type, array ('image', 'file '))){
Return $ this-> _ add ($ uploadlist, $ model );
} Else {
Return $ uploadlist [0] ['savename']; // return the name of the thumbnail.
}
}
/*
* The uploaded attachments are integrated into the data required by attach, stored in the table, and an array is returned.
**/
Private function _ add ($ uploadlist, $ module = ''){
// $ J = count ($ uploadlist );
$ V = array ();
Foreach ($ uploadlist as $ key => $ value ){
$ V [$ key] ['name'] = $ value ['name'];
$ V [$ key] ['savename'] = $ value ['savename'];
$ V [$ key] ['savepath'] = substr ($ value ['savepath'], 2 );
$ V [$ key] ['SIZE'] = $ value ['SIZE'];
$ V [$ key] ['userid'] = $ _ SESSION [C ('user _ AUTH_KEY ')];
$ V [$ key] ['uploadtime'] = time ();
$ V [$ key] ['alt'] =_ _ POST ['alt'] [$ key];
$ V [$ key] ['module'] = $ _ POST ['module']; // current project path
$ V [$ key] ['recordid'] = $ _ POST ['recordid']; // path of the current project
$ This-> add ($ v [$ key]);
If ($ this-> thumb ){
$ V [$ key] ['prefix'] = $ this-> thumbPrefix;
}
$ V [$ key] ['id'] = M ('Attach ')-> getLastInsID ();
}
Return $ v;
}
I hope this article will help you with ThinkPHP framework programming.