Welcome to the Linux community forum and interact with 2 million technical staff. Generally, we determine the file type based on the file extension. However, this is unreliable and we can easily avoid it by modifying the extension, it is generally necessary to read the file information for identification .? Php?filesarray(.test.jpg, test.png); $ fileTypesa
Welcome to the Linux community forum and interact with 2 million technical staff> generally, we judge the file type based on the file extension, but this is not reliable and we can easily avoid it by modifying the extension, generally, you must read the file information for identification. ? Php $ files = array ('./test.jpg', 'test.png '); $ fileTypes =
Welcome to the Linux community forum and interact with 2 million technicians>
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 ,'
';
}