简体   繁体   中英

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. Works fine except when I need to validate python files.

In my FileUploadController.php I have a store(FileStoreRequest $request) method that handles the upload. I added the $validatedData = $request->validate(); in it and it works.

I have also added the mimes.php in config folder with the following:

<?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

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

Any time I try to upload the python file I get the error

The preprocessor must be a file of type: py.

When I remove the mimes check from the rules() it passes.

The rules work, because I tested it on another view for zip file upload.

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',
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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