7
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 file mime starts with "image"
, to know if it is an image or not, since the mimes of images are usually image/png
, image/jpeg
, image/gif
...
But I was wondering if this would really be safe, besides having own concern if there is some other image type file that does not have the mime prefixed by image
.
I’d like to know:
- The example demonstrated for image verification is sufficient to maintain upload security or not?
What are the best ways to check image uploads in PHP?
Note: I am currently using the framework Silex, but I wouldn’t mind getting good suggestions using only pure php.
+1 of the hour your answer. I am only worried about the mime of AI (Abode Ilustrator), but there is another problem.
– Wallace Maxters
Yes, there are some programmers who use even file extensions to check upload, or put a validation in Javascript and think it’s good size (I was once one of them :p)
– Wallace Maxters
@Wallacemaxters I prefer to always limit with in_array.
– Guilherme Nascimento
But that’s what I’m going to have to do anyway. Why, in the specific case, I’m going to need to put
pdf
also. But this is off-topic, no need to modify your answer, since the context is only images.– Wallace Maxters
And where is the guarantee of being safe? It’s like doing or is it safe @Wallacemaxters?
– Jorge B.
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero