我如何验证图像(不在 $_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
}
总是返回真
有 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;
}