I'm making a system where I can receive an image upload.
To check on the server if the file type is image, I thought of this code:
if (strpos($upload->getClientMimeType(), 'image') !== 0) {
throw new UploadException(
sprintf('Extensão de arquivo %s é inválida', $upload->getClientOriginalExtension())
);
}
That is, checking if the mime of the file starts with "image"
, to know if it is an image or not, since image memos are usually image/png
, image/jpeg
, image/gif
...
But I was wondering if this really would be safe, as well as having your own concern if there is no other image-type file that does not have the mime prefixed by image
.
I would like to know:
- Is the example shown for image verification sufficient to maintain upload security or not?
What are the best ways to check image uploads in PHP?
Note : I'm currently using the framework > silex , but I would not mind receiving good suggestions using pure pure php .