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 imageメソッドがあります

/**
 * 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 に答える