Generally, we determine the file type based on the file extension, but this is very unreliable and can be easily avoided by modifying the extension. generally, we must read the file information for identification. & Lt ;? Php $ filesarray (& amp; #39;./test.jpg&=39},&=39;test.png & amp; #39;); $ fileTypes...
Generally, we determine the file type based on the file extension, but this is very unreliable and can be easily avoided by modifying the extension. generally, we must read the file information for identification.
$ Files = array ('./test.jpg', 'test.png ');
$ FileTypes = array (
7790 => 'exe ',
7784 => 'midi ',
8075 => 'Zip ',
8297 => 'rar ',
225216 => 'jpg ',
7173 => 'GIF ',
6677 => 'bmp ',
13780 => 'PNG ',
);
Foreach ($ files as $ file ){
$ Fp = fopen ($ file, 'RB ');
$ Bin = fread ($ fp, 2); // Read-Only header two bytes
Fclose ($ fp );
$ StrInfo = @ unpack ("C2chars", $ bin );
$ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']);
$ FileType = isset ($ fileTypes [$ typeCode])? $ FileTypes [$ typeCode]: 'Unknown ';
Echo $ file, 'type:', $ FileType ,'Code:', $ FileType ,'
';
}