Laravel 5在有效文件类型上验证CSV文件错误 [英] Laravel 5 validate csv file error on valid file type

查看:161
本文介绍了Laravel 5在有效文件类型上验证CSV文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用验证上传的csv文件

I am trying to validate an uploaded csv file with

$validator = Validator::make(
        [
            'file'      => $file,
            'extension' => strtolower($file->getMimeType()),
        ],
        [
            'file'      => 'required|in:csv',
        ]
    );

验证器无法提供带有消息The file must be a file of type: csv.

The validator fails on giving a valid csv file with message The file must be a file of type: csv.

如果我删除in:csv,则验证程序通过.

The validator passes if i remove in:csv.

我做错什么了吗?

PS-$file可用,并且具有标准上传文件类型object(Symfony\Component\HttpFoundation\File\UploadedFile)

PS - $file is available and of standard upload file type object(Symfony\Component\HttpFoundation\File\UploadedFile)

推荐答案

尝试更改

$validator = Validator::make(
    [
        'file'      => $file,
        'extension' => strtolower($file->getMimeType()),
    ],
    [
        'file'      => 'required|in:csv',
    ]
);

收件人

$validator = Validator::make(
    [
        'file'      => $file,
        'extension' => strtolower($file->getClientOriginalExtension()),
    ],
    [
        'file'          => 'required',
        'extension'      => 'required|in:csv',
    ]
);

这篇关于Laravel 5在有效文件类型上验证CSV文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆