Laravel 5 文件上传验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39854828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:33:31  来源:igfitidea点击:

Laravel 5 file upload validation

phplaravelvalidationfile-upload

提问by Mahendra Kurniawan

I'm using laravel built in file upload to upload video to my website. I've already successfully uploaded the file, but when my friend try to upload the video he got an error "validation.uploaded" i don't know why this is happening and i've been trying to find the error but i can't find it. We both try it in firefox and chrome but only my friend who got it. Please help this is my code:

我正在使用 laravel 内置文件上传将视频上传到我的网站。我已经成功上传了文件,但是当我的朋友尝试上传视频时,他收到错误“validation.uploaded”,我不知道为什么会发生这种情况,我一直在尝试查找错误,但我找不到没找到。我们都在 firefox 和 chrome 中尝试过,但只有我的朋友得到了它。请帮助这是我的代码:

Save Video Function

保存视频功能

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

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

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

    return $fullFileName;
}

Save From Form

从表单保存

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');
}

回答by Tohid Dadashnezhad

Actually if the size of file you try to download exceeds the "upload_max_filesize" the Laravel shows this validation error, so you must open the php.ini file and set the options like this:

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

post_max_size = 500M

post_max_size = 500M

upload_max_filesize = 500M

upload_max_filesize = 500M

Notice: if you use wamp or xamp don't forget to restart it otherwise rerun the command:

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

php artisan serve

php工匠服务

回答by yyc1217

Laravel responses 422 Unprocessable Entitywhich response body contains validation.uploadeddue to exceed upload size limitation. Try to set proper value for following possible parameters(e.g. 10M):

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

/etc/php.ini(PHP):

/etc/php.ini(PHP):

  • upload_max_filesize 10M
  • post_max_size 10M
  • upload_max_filesize 10M
  • post_max_size 10M

.htaccess(Apache):

.htaccess(阿帕奇):

  • php_value upload_max_filesize 10M
  • php_value post_max_size 10M
  • php_value upload_max_filesize 10M
  • php_value post_max_size 10M

Then restart Apache sudo systemctl restart httpd.service(Cent OS 7)

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