Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phpMick's avatar
Level 15

Validate file upload by file extension.

Is there a simple way to do this? I need to ensure that the file uploaded is .jrxml.

I guess that the mime type will be too generic.

Mick

0 likes
13 replies
Screenbeetle's avatar

Have you tried the mimtype validator? Something like:

'upload' => 'mimetypes:jrxml'
phpMick's avatar
Level 15

That fails and says that the file should be of type jrxml.

I don't think any of the Laravel stuff will work, I just need to check the extension somehow, in the rules.

Mick

Nash's avatar

@Screenbeetle @phpMick It should be mimes:jrxml instead of mimetypes:jrxml. This will check the extension.

"mimetypes" works too, but the full type must be specified, e.g. mimes:image/jpeg

phpMick's avatar
Level 15

I have:

$rules['template_file'] = 'required|file|mimes:jrxml';

This gives: file should be of type jrxml.

and

 $rules['template_file'] = 'required|file|mimetypes:application/octet-stream';

gives the file must be of type application/octet-stream

phpMick's avatar
Level 15

"I don't think any of the Laravel stuff will work, I just need to check the extension somehow, in the rules."

Screenbeetle's avatar

Hi again

I can only suggest something like grabbing the file extension as a string (using Jquery or php) and setting that into a field first. You can then pass that field in the request to the validator to check.

It's not elegant but should work.

phpMick's avatar
Level 15

Yeah I can easily add the extension to the Ajax request.

How then do I then make a rule for = "jrxml".

Yes I know it's never going to be elegant I just want to stop my users uploading random files :-)

Screenbeetle's avatar

Sorry - do you mean how to match 'jrxml' as a string?

I don't recall ever needing to do that now that you mention it. Is there a validator rule to match an input value to a specific string? You can probably do it with regex .. something like

'upload-extension' => ["required" , "regex:(jrxml)"] 

but that seems a bit OTT .. hmm, (brain not really in gear today thanks to the election)

Nash's avatar

$rules['template_file'] = 'required|file|mimes:jrxml';

@phpMick This should work just fine for checking file extensions? Did you remember to set the enctype="multipart/form-data" attribute for your <form> element? This is required when uploading files.

phpMick's avatar
Level 15

@Nash yes, but as stated above, this is not working.

<form class="form-horizontal" role="form"  enctype="multipart/form-data" files="true">
Nash's avatar

And you are using method="POST" and <input type="file" name="template_file">?

Sorry if the question seems silly. I'm just curious why the mimes rule won't work since I just recently used it without problem in one of my own projects.

phpMick's avatar
Level 15

It's because it's not a common minetype.

No, I'm not posting, I'm using Ajax.

Please or to participate in this conversation.