ThinkPHP Implementation of File Upload Function with Verification Code

  • 2021-07-24 10:28:05
  • OfStack

In this paper, an example is given to describe the file uploading function with verification code by ThinkPHP. Share it for your reference. The specific implementation method is as follows:

ThinkPHP upload file is very simple we only need to call a file upload class UploadFile can quickly achieve upload function, I will give you a file upload need to verify the function of the example, I hope the article will help you.

In the template, we just need to call

<html>
<head>
<title> Verification code </title>
</head>
<body>
<formaction='__URL__/login'method='post'enctype="multipart/form-data">
File upload :<inputtype='file'name='imgage'><br/>
Verification code :<inputtype='text'name='verify'>
<imgsrc='__URL__/verify'onclick='change()'id='verify'/><br/>
<inputtype='submit'value=' Submit '>
</form>
</body>
</html>

php processing file
<?php
classIndexActionextendsAction{
/* Verification code */
publicfunctionverify(){
$type=isset($_GET['type'])?$_GET['type']:'gif';// If you do not set the picture format of the verification code , Default to gif Format
import("@.ORG.Image");// Import picture out class
Image::buildImageVerify(4,1,$type);// How many digits are there to set the verification code , It's a number , Or letters
}
/* File upload */
publicfunctionupload(){
if(!empty($file))
import('@.ORG.UploadFile');// Import file upload class
$file=newUploadFile();// Instantiation UploadFile Class
// We can set the file upload 1 Attributes
$file->maxSize=1000000;// Set the size of uploaded pictures
$file->allowExtes=expload(',','jpg,jpeg,png,gif');// Set file upload format
$file->savePath='/Tpl/default/Public/Uploads/';// Set the picture storage location
$file->thumb='true';// Is it set to thumbnail
$file->thumbPrefix='s_';// Set the prefix of thumbnail
$file->thumbMaxWidth='400,100';// Set the maximum width of the picture
$file->thumbMaxHeight='400,100';// Set the maximum height of the 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(" The picture was uploaded successfully !");
}else{
$this->error(" Picture upload failed ");
}
}
}

Simple Analysis 1 Example

import('@.ORG.UploadFile');// Import file upload class 
$file=newUploadFile();// Instantiation UploadFile Class

This is a direct call to the thinkphp file processing class, we do not need to do any operations.
In the upload process, it will be different from other classes

$Model=M('Photo');
$data['image']=$_POST['image'];
$data['create_time']=time();
$vo=$Model->add($data);

This $_ POST ['image'] is to get the name of our html file, this can be an array, that is, multi-file upload.

I hope this article is helpful to everyone's ThinkPHP programming.


Related articles: