PHP find if file data is an image
Posted
by Christian Sciberras
on Stack Overflow
See other posts from Stack Overflow
or by Christian Sciberras
Published on 2010-06-02T09:46:03Z
Indexed on
2010/06/02
9:53 UTC
Read the original article
Hit count: 164
Imagine I have some file data in a variable $data. I need to determine whether it is an image or not. No need for details such as corrupt images etc.
Firs thought would be getting the file mime type by looking at the magic number and then see whether "image" is in the mime type.
No such luck, even if I have a "file extension to mime type" script, I don't have a reliable way to get mime from magic number.
My next option was to have a reasonable list of image file magic numbers and consult them. However, it relatively difficult to find such magic numbers (gif for instance has different magic numbers, some of which could pretty rare - if memory serves me right).
A better idea would be some linux program which can do this kind of thing.
Any ideas? I'm running RHEL and PHP 5.3. I've got root access - ie able to install stuff if needed.
- Chris.
© Stack Overflow or respective owner