MooseSaid's avatar

How to validate array of files?

I'm working on a project where every user is able to add multiple product images. I'm using a single form to submit the product which does 2 things in ProductController@store which are creating new product + creating images belongs to this product in Images table.

The important part is that I want to allow users to upload total of 10mb of images per product. So mainly I want to validate total files size of the array of images the user trying to update.

I found this solution: https://stackoverflow.com/questions/47054391/laravel-validate-array-of-files-total-allowable-upload-size/47056662#47056662

This solution is almost 5 years old so I was wondering if there is an easier way, or a more straight forward way to accomplish this?

0 likes
14 replies
axeloz's avatar

Maybe something like this can help:

$validator = Validator::make($request->all(), [
    'file' => Rule::forEach(function ($value, $attribute) {
        return [
            'required',
			'image'
        ];
    }),
]);
Sinnbeck's avatar

How about just a max of 1mb per image? That would make it alot easier as you can use built in validation

1 like
MooseSaid's avatar

@Sinnbeck but wouldn't make sense and would be a pain for users, what If I'm a user who would like to upload 1 High quality image not multiple images.

Sinnbeck's avatar

@MooseSaid then why have a limit at all?

Anyways. There isn't any built in way to validate this, so that 5 year old thread is a good start

1 like
MooseSaid's avatar

@Sinnbeck in order to avoid having customers uploading 1gb of photos for a single product :D Thanks!

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@MooseSaid thereby the limit :) 10mb per file then? And optimize images over 1 mb?

1 like
MooseSaid's avatar

@Sinnbeck I was thinking to start my project with limiting the overall size of images and leave it up to the user to upload whatever they want. But it's actually a very good idea to optimize imagies over 1mb, yet I have zero knowledge on how to accomplish that, I still have to research how to do that.

Sinnbeck's avatar

@MooseSaid there are packages for laravel to help you out. Just search laravel image optimize

1 like
MooseSaid's avatar

@Sinnbeck well, if you got a minute I just have a small question. I already manually wrote the logic and made my migrations but I just wanted to see if this is the best way to do it. I have Products Model which hasMany ProductImage, I'm using the same form to submit a post request to ProductController@store to validate both Product details and ProductImage files and within the same action Im creating a product and using ProductImage::Create() to create my images. So I don't think I will ever need controller for ProductImage because I'm using Product controller.

Is that considered a bad code or bad practice? If you think this deserves it's own thread with code snippets just let me know. Thank you so much for your time!

Sinnbeck's avatar

@MooseSaid that sounds completely fine. Perhaps I would later add a controller for adding and deleting images on an existing product. But what you have now sounds like how I would do it to

1 like
MichalOravec's avatar

@MooseSaid Really bad article... That code couldn't work for one important reason... Where is the reference to the stackoverflow?

1 like
MooseSaid's avatar

@MichalOravec I thought I included that. Fixed. Is that the only reason why you think it's bad? Is it a bad practice or not well written? Anyway, thanks!

Please or to participate in this conversation.