|
Posted by Toby A Inkster on 03/13/07 10:05
avlee wrote:
> i need to check if the file is png or jpeg.
> How can i do it ?
Open the file using fopen() and read in the first 10 bytes. Now close the
file. Match the first ten bytes using the following:
$is_image_png = preg_match('/^.PNG/', $ten_bytes);
$is_image_jpeg = preg_match('/(Exif|JFIF)$/', $ten_bytes);
Note, it's also fairly easy to check for a few other common file types:
$is_image_gif = preg_match('/^GIF8[79]/', $ten_bytes);
$is_audio_ogg = preg_match('/^Ogg/', $ten_bytes);
$is_script = preg_match('/^#!\//', $ten_bytes);
Of course, it's not a rigid test that the file really is of that type, but
it's a nice quick check, and should help you differentiate between JPEG
and PNG images fairly easily.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
Navigation:
[Reply to this message]
|