Limit number of files that can be uploaded

前端 未结 2 1331
礼貌的吻别
礼貌的吻别 2021-01-15 18:31

How can I limit the number of files that can be uploaded?

The max validation seems to apply to the size of the image (in kilobytes). How can I make a va

2条回答
  •  有刺的猬
    2021-01-15 19:11

    How I did in laravel 7.x

    Create a new form request class with the following command

    php artisan make:request UploadImageRequest

    use Illuminate\Foundation\Http\FormRequest;
    use App\Http\Requests\BaseFormRequest;
    
    class UploadImageRequest extends BaseFormRequest
    {
    public function authorize()
    {
      return true;
    }
    
    public function rules()
    {
      return [
        'coverImage.*' => 'image|mimes:png,jpg,jpeg,gif,svg|max:2048',
        'coverImage' => 'max:5',
      ];
    } 
    
    public function messages() {
      return [
        'coverImage.*.max' => 'Image size should be less than 2mb',
        'coverImage.*.mimes' => 'Only jpeg, png, bmp,tiff files are allowed.',
        'coverImage.max' => 'Only 5 images are allowed'
      ];
    }
    

    in View.blade.php

                            
    @error('coverImage')
     {{ $message }}
    @enderror
    

    in controller

    public function store(UploadImageRequest $request)
    {
      //code
    }
    

提交回复
热议问题