note 14864 deleted from function.getimagesize by jimw

From: Date: Mon, 10 Dec 2001 23:32:50 +0000
Subject: note 14864 deleted from function.getimagesize by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-22475@lists.php.net to get a copy of this message
Sometimes, relying on the extension (set by user) and mime-type (set by browser for incoming images) isn't enough. Here is a function that actually reads the image and checks the header bytes to make sure it's a valid jpg. Please note this was mostly written by a guy named g0thm0g. It gives a max of 250k so just change the size if you want more/less. function is_valid_jpg($file) { if (filesize($file) > (250*1024)) { return 0; } $fin = fopen($file, "rb"); if($fin) { $magic = fread($fin, 2); if((ord($magic[0]) != 0xFF) || (ord($magic[1]) != 0xD8)) { return 0; } $width = fread($fin, 2); $height = fread($fin, 2); $com = fread($fin, 1); if(ord($com) != 74) { return 0; } } else { return 0; } fclose($fin); return 1; }

« previous php.notes (#22475) next »