Laravel 5 file upload validation

12,815

Solution 1

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

Solution 2

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(e.g. 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)

Share:
12,815
Mahendra Kurniawan
Author by

Mahendra Kurniawan

Updated on June 13, 2022

Comments

  • Mahendra Kurniawan
    Mahendra Kurniawan almost 2 years

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