how to read the bytes that make up a file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to read the bytes that make up a file?

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

Thanks d11wtq

Your solution helped me overcome the problem
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

thank you,
I needed this one too :)
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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!
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

the header is:
'0x47', '0x49', '0x46'
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

thanks,
i tested some of my gif, and the first 6 bytes are the same.
Post Reply