当前位置:网站首页>Tp6 is easy to tread [original]
Tp6 is easy to tread [original]
2022-06-26 04:20:00 【Telkobe】
I am going to change the framework of the website from tp5 Switch to tp6, There are many small pits in the process , File uploading is one of them
First tp6 File upload is relative to tp5 In terms of the implementation process, the difference is still large , So I didn't succeed in trying to be lazy .
If the original code is uploaded in a similar way , Then you can continue to use , Basically, there is no need to change it .
$file = request()->file('pic');
$info = $file->move($path,' file name ');
The main reason is that there is a big difference when uploading and verifying .tp5 File objects have validation methods , and tp6 The unified verifier verification is adopted , So in the past, this kind of writing was used in tp6 Do not apply .
$file->validate(['size'=>500000,'ext'=>'jpg,png,gif'])->move($path);
Put a paragraph below tp6 Official manual file upload verification code .
public function upload(){
// Gets the form upload file
$files = request()->file();
try {
validate(['image'=>'filesize:10240|fileExt:jpg|image:200,200,jpg'])
->check($files);
$savename = [];
foreach($files as $file) {
$savename[] = \think\facade\Filesystem::putFile( 'topic', $file);
}
} catch (\think\exception\ValidateException $e) {
echo $e->getMessage();
}}
I copied this paragraph and tested it to be useless , If you copy the past , Then the result of the request will always be prompted :image Rule error , The last trace code found was filesize The name of the verification method in the verifier is fileSize, And it is precisely because of this that the corresponding error message should be returned when the error prompt is finally returned, but the default rule prompt array in the class ($this->typeMsg) Can't find filesize Corresponding value , So skip to the last prompt for rule error .
So the filesize Change to fileSize That's all right. . Some friends may not be used to try catch How to write it , Here I put my own writing .
$file = request()->file(' File domain field name ');
$v=\validate([' File domain field name '=>'fileSize:500000|fileExt:jpg,png,gif'])->failException(false);
if($v->check([' File domain field name '=>$file])){
$name=Filesystem::putFile($path,$file);
echo $name
}else{
echo $v->getError();
}
Helper functions input By default, there are no filter settings , So it's using input It needs to be set manually to obtain the data transmitted from the client input The third parameter of ( How to filter data ), Can be in app\Request Object filter Global filter properties ,app Under the table of contents Request file , Instead of applying profile settings , This is different from the previous version .
namespace app;
class Request extends \think\Request{
protected $filter = ['htmlspecialchars'];
}
边栏推荐
- 軟件調試測試的十大重要基本准則
- 35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
- R language and machine learning
- After four years of outsourcing, people are directly abandoned...
- Implementation of seven classes of BlockingQueue interface
- 【掘金运营套路揭露】真心被掘金的套路....
- Oracle 数据泵导表
- List of provinces, cities and counties in China
- China air compressor manufacturing market demand analysis and investment prospect research report 2022-2028
- Force buckle 515 Find the maximum value in each tree row
猜你喜欢
Implementation of seven classes of BlockingQueue interface
Microsoft prohibits Russian users from downloading and installing win10/11
Knowledge of SQL - database design, backup and restore
Conditional variables for thread synchronization
Unity移动端游戏性能优化简谱之 以引擎模块为划分的CPU耗时调优
MySQL index details
力扣 515. 在每个树行中找最大值
Gateway can not connect to tcp://127.0.0.1: Connection refused
A brain map to summarize the needs analysis (a supplement to the actual situation at work)
MySQL enable logbin in Qunhui docker
随机推荐
Oracle technology sharing Oracle 19.14 upgrade 19.15
What preparations should be made to develop an app from scratch
Unity mobile game performance optimization spectrum CPU time-consuming optimization divided by engine modules
Ubuntu installs PostgreSQL and uses omnidb to view
Upload script file (one sentence back door) WAF bypass (PHP)
VHDL design
[Qunhui] this suite requires you to start PgSQL adapter service
What preparation should I make before learning SCM?
小程序中实现视频通话及互动直播功能
The statistics in the MySQL field become strings, and then they are converted into numbers for sorting
线程同步之互斥量(互斥锁)
Double buffer technology asynchronous log system
[Qunhui] import certificate
[Nuggets' operation routine disclosure] the routine of being truly Nuggets
Yolov5 improvements: replace the backbone
Nailing open platform - applet development practice (nailing applet server side)
線程同步之讀寫鎖
1. foundation closing
Your requirements could not be resolved
Principle and implementation of syn cookie