图像验证以一种形式工作,但在Laravel

时间:2017-02-09 12:46:48

标签: php laravel

我有这样的事情:

myitemrequest.php:

  public function authorize()
    {
        return true;
    }

 public function rules()
    {
        return [
            'avatar_pic' => 'image',
        ];
    }

验证了这一点:

 if ($validator->fails()) {
                 return back()
                ->withErrors($validator)
                ->withInput();
        }
if(request()->file("avatar_upload")) {
      $file = request()->file("avatar_upload");
     $file->storeAs('public/avatars/' . Auth::user()->id , "avatar.jpg");
}
return back();

这完美无缺。现在出于某种原因:

otherequest.php:

public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'animage' => 'image',
        'anotherimage' => 'image',
        'anotherimage2' => 'image',
        'anotherimage3' => 'image'
    ];
}

验证了这一点:

  if ($backgroundImagesRequest->authorize() == false) {
        return back()
            ->withErrors($worklow_request)
            ->withInput();
    }

    if(isset($backgroundImagesRequest->animage))
    {

        $file = request()->file("animage");
        $file->storeAs('public/background_images/' . Auth::user()->id , "animage.jpg");
    }

    if(isset($backgroundImagesRequest->anotherimage1))
    {

        $file = request()->file("anotherimage1");
        $file->storeAs('public/background_images/' . Auth::user()->id , "anotherimage1.jpg");
    }

    if(isset($backgroundImagesRequest->anotherimage2))
    {

        $file = request()->file("anotherimage2");
        $file->storeAs('public/background_images/' . Auth::user()->id , "anotherimage2.jpg");
    }

    if(isset($backgroundImagesRequest->anotherimage3))
    {

        $file = request()->file("anotherimage3");
        $file->storeAs('public/background_images/' . Auth::user()->id , "anotherimage3.jpg");
    }

    return back();
}

无法工作,它只是给我发回错误信息"文件必须是图像!"。我显然尝试使用与头像上传相同的图像,它完美地运行,并且相应地进行验证和存储。它只是在背景图像中不会发生。

任何想法可能出错?

1 个答案:

答案 0 :(得分:2)

在你的第一个"工作"您有验证的例子

if ($validator->fails()) {
    return back()
    ->withErrors($validator)
    ->withInput();
}

第二,你得到了

if ($backgroundImagesRequest->authorize() == false) {
    return back()
    ->withErrors($worklow_request)
    ->withInput();
}

替换该代码,因为两个示例中的验证都不相同