当前位置:网站首页>Tp6 is easy to tread [original]

Tp6 is easy to tread [original]

2022-06-26 04:20:00 Telkobe

Link to the original text

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 .

1611308552470384.png

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'];
}

原网站

版权声明
本文为[Telkobe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180535384737.html