Laravel MIME 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19849702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel MIME Validation
提问by Tarik
I came across a hassle, regarding validating the upload of JavaScript files using Laravel, where the valdation rule is :
我遇到了一个麻烦,关于使用 Laravel 验证 JavaScript 文件的上传,其中验证规则是:
'javascript_file' => 'required|mimes:js'
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
这应是据我所知工作,因为Laravel使用mime_content_type()来猜测文件的默剧,但与文件进行测试时它不经历,给了我一个MIME类型错误application/javascript
的MIME类型
Edit: dd($_FILES)
gives
编辑: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)
Edit 2:
编辑2:
As @searsaw pointed, seems like the validation guess was wrong.
正如@searsaw 所指出的,似乎验证猜测是错误的。
And after digging in validateMimes
method in vendor\laravel\framework\src\Illuminate\Validation\Validator.php
在挖掘validateMimes
方法之后vendor\laravel\framework\src\Illuminate\Validation\Validator.php
and dumping the guess variable dd($value->guessExtension())
i got a txt
mime -_- which worked
并转储猜测变量dd($value->guessExtension())
我得到了一个txt
哑剧-_-它起作用了
采纳答案by searsaw
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!
好的。所以在对 Laravel 的源代码进行彻底的挖掘之后,我已经弄清楚了这个系统是如何工作的。本质上,Validator 根据您传递的参数分解规则,在这种情况下,将它们发送到类validateMimes
上的方法Validator
。这会调用一个猜测器来找出文件的扩展名。猜测器首先通过循环遍历一堆其他猜测器finfo
来猜测 mime 类型,这些猜测器使用PHP 扩展来猜测 mime 类型。一旦它具有 mime-type,它就会将该 mime-type 传递给扩展猜测器,该猜测器根据以 mime-type 作为键和扩展作为值的数组来猜测扩展。然后它将扩展返回到原始调用中Validator
class 查看扩展名是否是您首先传递给规则的“参数”数组中的值。呼!
Here is the entry the extension guesser is using to guess the extension based on mime-type.
这是扩展猜测者用来根据 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',
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.
正中间是 javascript 条目。根据我收集到的信息,我假设哑剧式猜测者猜错了。它可能将其解释为文本文件而不是 javascript。尝试确保文件附加了正确的 MIME 类型。
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.
我测试了一个普通的 javascript 文件在 using 时出现了什么mime_content_type()
,它返回了text/plain
. 我猜这也是 Laravel 正在做的事情。
Hope this helps!
希望这可以帮助!
回答by Aji Aini Hanif
Well, if you're on Laravel >= 5.2, there's a new validation method called mimetypes. So, for your case it could be:
好吧,如果你在 Laravel >= 5.2 上,有一个新的验证方法叫做 mimetypes。因此,对于您的情况,它可能是:
'javascript_file' => 'required|mimetypes:application/javascript,text/plain',