Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different max file size for different file types?

I want to set different max file sizes for different file types, like:

  • 10 MB max file size for images (PNG, JPEG, and GIF)
  • 50 MB max file size for videos (MP4)

Right now, my validator only allows 10 MB for all file types:

$validator = Validator::make($request->all(), [
    'file' => 'required|max:10000|mimes:jpg,jpeg,png,gif,mp4',
]);

How can I set different max file sizes for different file types?

like image 618
user698518 Avatar asked Feb 22 '26 16:02

user698518


1 Answers

You can solve this question with the using if condition rule. I know, it is not best practice but it could solve your issue.

$default_max_value = 10000;
if($request->hasFile('file') && $request->get('file')->getClientOriginalExtension() == 'mp4'){
    $default_max_value = 50000;
}

$validator = Validator::make($request->all(), [
    'file' => 'required|max:'.$default_max_value.'|mimes:jpg,jpeg,png,gif,mp4',
]);
like image 152
bl4cksta Avatar answered Feb 24 '26 04:02

bl4cksta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!