Laravel MIME验证 [英] Laravel MIME Validation

查看:105
本文介绍了Laravel MIME验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用Laravel验证JavaScript文件的上传,我遇到了麻烦, 评估规则为:

I came across a hassle, regarding validating the upload of JavaScript files using Laravel, where the valdation rule is :

'javascript_file' => 'required|mimes:js'

据我所知,这应该起作用,因为Laravel使用 mime_content_type()来猜测哑剧的文件,但没有通过,在测试application/javascript mime type

Which should work as far as i know, since Laravel uses the mime_content_type() to guess the mimes of files, but it doesn't go through, giving me a mime type error when testing with a file with application/javascript mime type

dd($_FILES)提供

["name"]=> string(7) "data.js"
["type"]=> string(22) "application/javascript"
["tmp_name"]=> string(35) "C:\easyphp\binaries\tmp\php21D0.tmp"
["error"]=> int(0)
["size"]=> int(12253)

正如@searsaw指出的那样,似乎验证猜测是错误的.

As @searsaw pointed, seems like the validation guess was wrong.

并且在vendor\laravel\framework\src\Illuminate\Validation\Validator.php

并转储猜测变量dd($value->guessExtension())我得到了txt哑剧-_-起作用

and dumping the guess variable dd($value->guessExtension()) i got a txt mime -_- which worked

推荐答案

好.因此,在深入研究Laravel的源代码之后,我已经弄清楚了该系统的工作原理.本质上,Validator会根据您传递的参数分解规则,在这种情况下,将其发送到Validator类的validateMimes方法.这将调用一个猜测器来找出文件的扩展名.猜测者首先通过循环其他猜测者来猜测mime类型,这些猜测者使用finfo PHP扩展来猜测mime类型.一旦具有mime-type,它将把该mime-type传递给扩展猜测器,该猜测器根据具有mime-type作为键和扩展名作为值的数组来猜测扩展.然后,它将扩展名返回到Validator类中的原始调用,以查看扩展名是否是您首先传递给规则的参数"数组中的值. !

Ok. So after a thorough dig through the source code of Laravel, I have figured out how this system works. Essentially, Validator breaks down the rule from the parameters you passed and, in this case, sends them to the validateMimes method on the Validator class. This calls a guesser to find out the extension on the file. The guesser first guesses the mime-type by cycling through a bunch of other guessers, which use the finfo PHP extension to guess the mime-type. Once it has the mime-type, it passes that mime-type to an extension guesser that guesses extensions based on an array that has the mime-type as the key and the extension as the value. Then it returns the extension to the original call in the Validator class looks to see if the extension is a value in the array of "parameters" you passed to the rule in the first place. Phew!

这是扩展程序猜测程序用于根据mime类型猜测扩展程序的条目.

Here is the entry the extension guesser is using to guess the extension based on mime-type.

'application/java-archive' => 'jar',
'application/java-serialized-object' => 'ser',
'application/java-vm' => 'class',
'application/javascript' => 'js',
'application/json' => 'json',
'application/jsonml+json' => 'jsonml',

位于中间的是javascript条目.根据我收集到的信息,我假设哑剧类型的猜测者猜测错了.它可能会将其解释为文本文件,而不是javascript.尝试确保该文件已附加正确的mime类型.

Right in the middle is the javascript entry. From the information I have gathered, I am assuming the mime-type guesser is guessing wrong. It may be interpreting it as a text file and not javascript. Try making sure the file has the right mime-type attached to it.

我测试了使用mime_content_type()出现的普通javascript文件,并返回了text/plain.我想这也是Laravel在做的事情.

I tested what a plain javascript file comes up as using mime_content_type(), and it returned text/plain. I'm guessing it's what Laravel is doing too.

希望这会有所帮助!

这篇关于Laravel MIME验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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