Reputation: 20223
I have a file upload function in my Symfony2 project and I would like to validate that the uploaded file is a .tex
file format => LaTex file.
For text/html, I am using:
$metadata->addPropertyConstraint('file', new Assert\File(array(
'maxSize' => '100000k',
'mimeTypes' => array("text/html"),
'mimeTypesMessage' => 'Please upload a valid HTM/HTML File',
)));
I am trying:
$metadata->addPropertyConstraint('file', new Assert\File(array(
'maxSize' => '100000k',
'mimeTypes' => array(
'text/html',
'application/x-tex',
'application/x-latex'
),
'mimeTypesMessage' => 'Please upload a valid HTM/HTML/TEX File',
)));
But as soon as I am uploading a .tex file, I am getting the message:
Please upload a valid HTM/HTML/TEX File.
How can I add Tex file also here? Thank you.
Upvotes: 7
Views: 2084
Reputation: 20201
I think that the same principles of image validation can be applied here. It's unreliable to trust mime-type since user could plant php
executable within image/jpeg
.
For the images this can be verified by using getimagesize()
function. As for the LaTeX
you would need some 3rd-party lib what will try to open uploaded file and throw the exception on error (invalid file format, invalid expression, etc...).
Maybe this one: PHPLatex?
Also, which mime-type did you get when you tried uploading file? (@Ahmed's idea)
Upvotes: 4
Reputation: 4730
Unless the submitting client 'knows' that tex files are registered as application/x-tex I believe that the client won't sent then as such.
Can you search for the \documentclass
directive within the stream?
Alternatively if you are in control of the client machines, e.g. at work or university, can you get the mime types registered on them?
Upvotes: 2