简体   繁体   English

对于python文件,Laravel 5.4文件上载验证失败

[英]Laravel 5.4 file upload validation fails for python files

I'm trying to figure out what is wrong with my validation, but I'm not sure. 我想弄清楚我的验证有什么问题,但我不确定。

I have created a file upload that uploads the file to S3. 我创建了一个文件上传文件,将文件上传到S3。 Works fine except when I need to validate python files. 除非我需要验证python文件,否则工作正常。

In my FileUploadController.php I have a store(FileStoreRequest $request) method that handles the upload. 在我的FileUploadController.php我有一个处理上传的store(FileStoreRequest $request)方法。 I added the $validatedData = $request->validate(); 我添加了$validatedData = $request->validate(); in it and it works. 在它,它的工作原理。

I have also added the mimes.php in config folder with the following: 我还在config文件夹中添加了mimes.php ,其中包含以下内容:

<?php

return [
  'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
  'py' => array('text/plain', 'application/x-python' , 'application/octet-stream, application/x-python-code, text/x-python-script', 'text/x-python'),
];

And the rules() method inside my FileStoreRequest class is 而我的FileStoreRequest类中的rules()方法是

public function rules()
    {
        return [
            'preprocessor' => 'mimes:py',
        ];
    }

Any time I try to upload the python file I get the error 每当我尝试上传python文件时,我都会收到错误消息

The preprocessor must be a file of type: py. 预处理器必须是类型为py的文件。

When I remove the mimes check from the rules() it passes. 当我从rules()删除mimes check时它会通过。

The rules work, because I tested it on another view for zip file upload. 规则有效,因为我在另一个视图上测试了zip文件上传。

Any ideas what could be wrong? 什么想法可能是错的?

You can create custom validation like: 您可以创建自定义验证,如:

$input = $request->all();

if (isset($input["preprocessor"]) && !empty($input["preprocessor"])) {        
$filesource = $input["preprocessor"];
$fileExtension = $filesource->getClientOriginalExtension();
$input["ext"] = $fileExtension;
} 

$rules = array(
'ext' => 'nullable|in:py',
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM