验证 Laravel 4 上的指定图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15705086/
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
validate specified image on laravel 4
提问by alioygur
how can i validate an image (not in $_FILES)
我如何验证图像(不在 $_FILES 中)
this is not work
这不行
$input = array('image' => 'image.txt');
$rules = array('image' => array('Image'));
$validator = Validator::make($input, $rules);
if($validator->fails()){
return $validator->messages();
} else {
return true
}
always return true
总是返回真
There is Laravel validate image methods
有 Laravel 验证图像方法
/**
* Validate the MIME type of a file is an image MIME type.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateImage($attribute, $value)
{
return $this->validateMimes($attribute, $value, array('jpeg', 'png', 'gif', 'bmp'));
}
/**
* Validate the MIME type of a file upload attribute is in a set of MIME types.
*
* @param string $attribute
* @param array $value
* @param array $parameters
* @return bool
*/
protected function validateMimes($attribute, $value, $parameters)
{
if ( ! $value instanceof File or $value->getPath() == '')
{
return true;
}
// The Symfony File class should do a decent job of guessing the extension
// based on the true MIME type so we'll just loop through the array of
// extensions and compare it to the guessed extension of the files.
foreach ($parameters as $extension)
{
if ($value->guessExtension() == $extension)
{
return true;
}
}
return false;
}
回答by Mirko Akov
To validate a file, you have to pass the $_FILES['fileName']
array to the validator.
要验证文件,您必须将$_FILES['fileName']
数组传递给验证器。
$input = array('image' => Input::file('image'));
and I am pretty sure that your validation rules must be lowercase.
我很确定您的验证规则必须是小写的。
$rules = array(
'image' => 'image'
);
Notice that I have the removed the array from the value.
请注意,我已从值中删除了数组。
For more information, check out the validation docs
有关更多信息,请查看验证文档
回答by Alex Bouma
Also make sure you open the form for files!
还要确保打开文件表单!
Make sure it has the enctype="multipart/form-data"
attribute in the from tag.
确保它enctype="multipart/form-data"
在 from 标签中具有该属性。