I need to find what kind of file user have upload by checking binary data, and I found perfect solution for that, over here
Just to be specific this is the function I’m using:
function getImgType($filename) {
$handle = @fopen($filename, 'r');
if (!$handle)
throw new Exception('File Open Error');
$types = array('jpeg' => "\xFF\xD8\xFF", 'gif' => 'GIF', 'png' => "\x89\x50\x4e\x47\x0d\x0a", 'bmp' => 'BM', 'psd' => '8BPS', 'swf' => 'FWS');
$bytes = fgets($handle, 8);
$found = 'other';
foreach ($types as $type => $header) {
if (strpos($bytes, $header) === 0) {
$found = $type;
break;
}
}
fclose($handle);
return $found;
}
Now my question is, how can I get bits for other file types, like .zip, .exe, mp3, mp4 etc… if there is some kind of list somewhere out there it would be great, though I would like to extract it myself and learn how all of this really works.
What you’re looking for is called file magic number.
The magic number is a type of file signature – since sometimes it takes more than the magic number to identify the file.
A (very) short list of such numbers can be found here. A larger list can be found here.
File identification websites often times also mention the file magic number.
In linux, the
filecommand can be used to identify files.In PHP you can use the FileInfo set of functions to identify files.
By the way, you did not specify the kind of files you want to identify. Sometimes, identification might be the wrong solution. For example, people used to want to identify files before passing them to GD or saving them on the server as images.
In this case, identification is not really your job. Instead, use the following code: