thinkphp implementation of file upload with verification code instance _php instance

Source: Internet
Author: User

The example of this article describes the implementation of thinkphp with verification code file upload function. Share to everyone for your reference. The implementation methods are as follows:

thinkphp upload file is very simple we just call a file upload class UploadFile can quickly upload the function, I would like to give you a file upload the need to verify the function of the example, I hope the article will bring help to everyone.

In the template we just need to call on it

Copy Code code as follows:
<title> Verification Code </title>
<body>
<formaction= ' __url__/login ' method= ' post ' enctype= ' Multipart/form-data ' >
File upload: <inputtype= ' file ' name= ' Imgage ' ><br/>
Verification code: <inputtype= ' text ' name= ' verify ' >
<br/>
<inputtype= ' Submit ' value= ' submitted ' >
</form>
</body>

PHP Processing files
Copy Code code as follows:
<?php
classindexactionextendsaction{
/* Verification Code * *
Publicfunctionverify () {
$type =isset ($_get[' type '])? $_get[' type ': ' gif ';//If you do not set the picture format for the CAPTCHA, the default is GIF format
Import ("@.org.image");//Imported Picture out class
Image::buildimageverify (4,1, $type);/set verification code Several, is the number, or the letter
}
* * File Upload/*
Publicfunctionupload () {
if (!empty ($file))
Import (' @.org.uploadfile ')//File Upload class
$file =newuploadfile ();//Instantiate UploadFile class
We can set some properties for file uploads
$file->maxsize=1000000;//settings upload picture size
$file->allowextes=expload (', ', ' jpg,jpeg,png,gif ');//Set File upload format
$file->savepath= '/tpl/default/public/uploads/';//Set Picture location
$file->thumb= ' true ';//is set to thumbnail
$file->thumbprefix= ' s_ ';//Set the prefix of the thumbnail
$file->thumbmaxwidth= ' 400,100 ';//Set the maximum width of the picture
$file->thumbmaxheight= ' 400,100 ';//set maximum height of picture
if ($file->upload) {
$list = $file->getuploadfileinof ()//Get File upload information
Import (' @.org.image ');
Add a watermark to a picture
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 ("Picture upload success!");
}else{
$this->error ("Picture upload failed");
}
}
}

Simply analyze the example

Copy Code code as follows:
Import (' @.org.uploadfile ')//File Upload class
$file =newuploadfile ();//Instantiate UploadFile class

This is a direct call to the Thinkphp file processing class, we do not need to do any operation.
In the process of uploading, it will be different from other classes.

Copy Code code as follows:
$Model =m (' Photo ');
$data [' image ']=$_post[' image '];
$data [' Create_time ']=time ();
$vo = $Model->add ($data);

This $_post[' image ' is the name of our HTML file, which can be an array that's multiple files uploaded.

I hope this article will help you with the thinkphp program design.

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.