note 14864 added to function.getimagesize.php

From: Date: Fri, 17 Aug 2001 23:45:34 +0000
Subject: note 14864 added to function.getimagesize.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-16860@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; } http://www.php.net/manual/en/function.getimagesize.php

« previous php.notes (#16860) next »