0

我如何验证图像(不在 $_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;
}
4

2 回答 2

2

要验证文件,您必须将$_FILES['fileName']数组传递给验证器。

$input = array('image' => Input::file('image'));

而且我很确定您的验证规则必须是小写的。

$rules = array(
    'image' => 'image'
);

请注意,我已从值中删除了数组。

有关更多信息,请查看验证文档

于 2013-03-29T15:53:19.493 回答
0

还要确保您打开文件的表格!

确保它enctype="multipart/form-data"在 from 标记中具有该属性。

于 2013-04-29T19:59:46.833 回答