简体   繁体   English

如何在Laravel Validator中添加MIME类型

[英]How to add a mime type in Laravel Validator

I can not figure out how to add a mime type before validation. 我无法弄清楚如何在验证之前添加mime类型。 I need to upload CSV files however they have the mime type text/x-comma-separated-values so how can I add that mime type to the MimeTypeExtensionGuesser array? 我需要上传CSV文件,但是它们具有mime类型的text / x-逗号分隔值,因此如何将那个mime类型添加到MimeTypeExtensionGuesser数组中? I would like to keep things simple in my Controller method and just have: 我想在我的Controller方法中保持简单,只需:

$validator = Validator::make(Input::all(), [
'file' => 'required|mimes:csv'
]);

but first I need to add the new mime type. 但首先我需要添加新的mime类型。

I've got a little bit different approach: just get file extension from file name and use in rule. 我有一些不同的方法:从文件名获取文件扩展名并in规则中使用。 Here is my full answer to similar question - https://stackoverflow.com/a/26299430/1331510 这是我对类似问题的完整答案-https: //stackoverflow.com/a/26299430/1331510

$mime = Input::file('file')->getMimeType();//returns text/plain

    //check if file is csv

    if($mime != 'text/plain'){

        $error = array();
        $error[] = "File type must be csv";
        $validator = $error;
        return Redirect::to('your-page')->withErrors($validator);
    }

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

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