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
how to read the bytes that make up a file?
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
10 bytes right?
Untested but.... (lemme know how it goes
)
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
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';
}
?>