PHP binary file types

Source: Internet
Author: User
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 ,'
';

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.