如何验证图像(不在$ _FILES中)
这不起作用
$input = array('image' => 'image.txt');
$rules = array('image' => array('Image'));
$validator = Validator::make($input, $rules);
if($validator->fails()){
return $validator->messages();
} else {
return true
}
始终返回true
有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;
}
答案 0 :(得分:2)
要验证文件,您必须将$_FILES['fileName']
数组传递给验证器。
$input = array('image' => Input::file('image'));
我非常确定您的验证规则必须是小写的。
$rules = array(
'image' => 'image'
);
请注意,我已从值中删除了数组。
有关详细信息,请查看validation docs
答案 1 :(得分:0)
还要确保打开文件表单!
确保from标记中包含enctype="multipart/form-data"
属性。