简体   繁体   中英

Laravel 5 file upload validation

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:

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

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:

post_max_size = 500M

upload_max_filesize = 500M

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

php artisan serve

Laravel responses 422 Unprocessable Entity which response body contains validation.uploaded due to exceed upload size limitation. Try to set proper value for following possible parameters(eg 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM