dongwei8440 2019-04-01 10:53 采纳率: 100%
浏览 81

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

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?

  • 写回答

1条回答 默认 最新

  • doudui5753 2019-04-01 12:07
    关注

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

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数