ThinkPHP implements the file upload function with verification code _ php instance

Source: Internet
Author: User
This article mainly introduces ThinkPHP's implementation of the file upload function with verification code. it is a very practical technique to import and call the instance-related classes, for more information about how ThinkPHP uploads files with verification codes, see the example below. Share it with you for your reference. The specific implementation method is as follows:

ThinkPHP is very simple to upload files. we only need to call a file Upload class UploadFile to quickly implement the upload function. below I will give you an example of the function to be verified when uploading files, I hope the article will help you.

In the template, we only need to call it.

The code is as follows:



Verification code





Php file processing

The code is as follows:

<? Php
ClassIndexActionextendsAction {
/* Verification code */
Publicfunctionverify (){
$ Type = isset ($ _ GET ['type'])? $ _ GET ['type']: 'GIF'; // If the image format of the verification code is not set, the default format is gif.
Import ("@. ORG. Image"); // import the Image class
Image: buildImageVerify (, $ type); // you can specify the number or letter of the verification code.
}
/* File Upload */
Publicfunctionupload (){
If (! Empty ($ file ))
Import ('@. ORG. uploadfile'); // import file Upload class
$ File = newUploadFile (); // instantiate the UploadFile class
// We can set some attributes of file upload.
$ File-> maxSize = 1000000; // you can specify the size of the uploaded image.
$ File-> allowExtes = expload (',', 'jpg, jpeg, png, gif '); // sets the file upload format
$ File-> savePath = '/Tpl/default/Public/Uploads/'; // set the image storage location
$ File-> thumb = 'true'; // whether to set it to a thumbnail
$ File-> thumbPrefix ='s _ '; // you can specify the prefix of a thumbnail.
$ File-> thumbMaxWidth = '000000'; // you can specify the maximum width of an image.
$ File-> thumbMaxHeight = '000000'; // you can specify the maximum image height.
If ($ file-> upload ){
$ List = $ file-> getUploadFileInof (); // Obtain the file upload information
Import ('@. ORG. image ');
// Add a watermark to the image
Image: water ($ list [0] ['savepath'].'s _'. $ list [0] ['savename'], 'File/Tpl/defalut/Public/Images/logo.jpg ');
} Else {
$ This-> error ($ file-> getErrorMsg ());
}
$ Model = M ('photo ');
$ Data ['image'] = $ _ POST ['image'];
$ Data ['create _ time'] = time ();
$ Vo = $ Model-> add ($ data );
If ($ vo! = False ){
$ This-> success ("image uploaded successfully! ");
} Else {
$ This-> error ("image Upload failed ");
}
}
}


Simple Analysis example

The code is as follows:

Import ('@. ORG. uploadfile'); // import file Upload class
$ File = newUploadFile (); // instantiate the UploadFile class


This directly calls the thinkphp file processing class, and we do not need to perform any operations.
There are some differences with other classes during the upload process.

The code is as follows:

$ Model = M ('photo ');
$ Data ['image'] = $ _ POST ['image'];
$ Data ['create _ time'] = time ();
$ Vo = $ Model-> add ($ data );


This $ _ POST ['image'] is used to obtain the name of our html file. this can be an array, that is, multifile Upload.

I hope this article will help you with ThinkPHP programming.

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.