user2394156
user2394156

Reputation: 1840

File extension validation in Symfony3

Is there any already implemented way to validate file extension in Symfony? File and Image validators can only validate mime type. Is there any way to do it or do I have to use a custom validator / callback?

Upvotes: 0

Views: 818

Answers (1)

Jelle Kapitein
Jelle Kapitein

Reputation: 71

You could use a RegexValidator if all you want to validate about is the file extension. Do keep in mind that a file extension in no way guarantees the contents of the file. Someone could upload an executable as a .png for instance.

Example

new RegexValidator('/(.+)(\.jpg|\.png|\.gif)$/')

Will match any filename ending in .jpg, .png or .gif

If you want more control over it (for example, passing an array of allowed extensions) I would recommend using a CallbackValidator or making your own. Good luck!

Upvotes: 2

Related Questions