PHP uses the binary file header to quickly determine the file type reference: @ we generally determine the file type according to the file extension, but this is very unreliable and can be easily avoided by modifying the extension, generally, you must read the file information for identification. & Lt ;? Php?filesarray(.test.jpg, Test.png); $ fileTypesarray (7790 & gt; exe, PHP takes the binary file header to quickly determine the file type
Reference: @
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.
'Exe ', 7784 => 'midi', 8075 => 'Zip ', 8297 => 'rar', 225216 => 'jpg ', 7173 => 'GI ', 6677 => 'bmp ', 13780 => 'PNG',); foreach ($ files as $ file) {$ fp = fopen ($ file, 'RB '); $ bin = fread ($ fp, 2); // read-only first 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 ,'
';}