Laravel 5文件上传验证

时间:2016-10-04 14:24:00

标签: php laravel validation file-upload

我使用laravel内置文件上传功能将视频上传到我的网站。我已经成功上传了该文件,但当我的朋友尝试上传视频时,他收到了错误" validation.uploaded"我不知道为什么会这样,我一直试图找到错误但我无法找到它。我们都在firefox和chrome中试用它,但只有我的朋友才能得到它。请帮助这是我的代码:

保存视频功能

protected function saveVideo(UploadedFile $video)
{
    $fileName = str_random(40);

    $fullFileName = $fileName.'.'.$video->guessClientExtension();

    $video->storeAs('videos', $fileName);

    return $fullFileName;
}

保存自表单

public function update(Request $request, $id)
{
    //dd($request->all());
    $video = Video::findOrFail($id);
    $this->validate($request, [
        'title' => 'required|unique:lessons,title,'.$video->id,
        'lesson_lists' => 'required',
        'description' => 'required'
    ]);
    $data = $request->only('title', 'description','lesson_lists');
    $data['slug'] = str_slug($request->title);

    if($request->hasFile('image')){
        $data['image'] = $this->saveImage($request->file('image'));
        if($video->image !== '') $this->deletePhoto($video->image);
    }

    if($request->hasFile('video')){
        $data['video'] = $this->saveVideo($request->file('video'));
        if($video->video !== '') $this->deleteVideo($video->video);
    }

    //$data['lesson_id'] = implode($request->get('lesson_lists'));
    $video->update($data);
    Session::flash("flash_notification", [
        "level"=>"success",
        "message"=>"Berhasil menyimpan $video->title"
    ]);

    return redirect()->route('video.index');
}

3 个答案:

答案 0 :(得分:5)

实际上,如果您尝试下载的文件大小超过“upload_max_filesize”,Laravel会显示此验证错误,因此您必须打开php.ini文件并设置如下选项:

post_max_size = 500M

upload_max_filesize = 500M

注意:如果您使用wamp或xamp,请不要忘记重新启动它,否则请重新运行命令:

php artisan serve

答案 1 :(得分:0)

Laravel响应 422 Unprocessable Entity ,由于超出上传大小限制,响应正文包含validation.uploaded。尝试为以下可能的参数设置适当的值(例如10M):

/etc/php.ini(PHP):

  • upload_max_filesize 10M
  • post_max_size 10M

.htaccess(Apache):

  • php_value upload_max_filesize 10M
  • php_value post_max_size 10M

然后重启Apache sudo systemctl restart httpd.service(Cent OS 7)

答案 2 :(得分:-1)