Page 1 of 1

how to read the bytes that make up a file?

Posted: Tue May 10, 2005 10:02 am
by jasongr
Hello

I have a file which the user uploads
The file should be of type jpeg
I would like to ensure that the uploaded file has indeed the correct header format of jpeg: '\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46'

I need some way to read the first set of bytes that make up the file
and then to compare them to the correct header of
'\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46'

does anyone know how I can read these bytes and perform the comparison?

thanks in advance

Posted: Tue May 10, 2005 10:49 am
by Chris Corbyn
10 bytes right?

Untested but.... (lemme know how it goes ;-) )

Code: Select all

<?php

function isJpeg($file) {
	
	$hex_chars = array ( //Each byte in JPEG header
		0xFF,
		0xD8,
		0xFF,
		0xE0,
		0x00,
		0x10,
		0x4A,
		0x46,
		0x49,
		0x46
	); //End Array
	
	$handle = fopen($file, 'r'); //Open file read-only
	
	for ($i=0; ($i<10 && !feof($handle)); $i++) { //Not at end of file and up to tenth byte
		
		$bin = (fread($handle, 1)); //Read next byte in file
		$hex = bin2hex($bin); //Convert to hex
		$dec = hexdec($hex);
		
		if ($dec != $hex_chars[$i]) {
			return false;
		} //End if
				
	} //End for loop
	
	return true;
}

/* TEST */
$image = './file.ext';

if (isJpeg($image)) {
	echo 'File is a JPEG';
} else {
	echo 'File is not a JPEG or is corrupt';
}

?>
EDIT | Yes, this works (I'm gonna work with this and make it more generic to test any file type) - I'll put it in snippets when I'm done

Posted: Tue May 10, 2005 12:08 pm
by jasongr
Thanks d11wtq

Your solution helped me overcome the problem

Posted: Tue May 10, 2005 12:53 pm
by hongco
thank you,
I needed this one too :)

Posted: Tue May 10, 2005 5:01 pm
by hongco
i checked for gif, and it seems the header of gif file is the first 6 bytes...please let me know if i am correct/wrong.

thanks!

Posted: Tue May 10, 2005 5:10 pm
by jasongr
the header is:
'0x47', '0x49', '0x46'

Posted: Tue May 10, 2005 6:35 pm
by hongco
thanks,
i tested some of my gif, and the first 6 bytes are the same.