PHP uses the binary file header to quickly determine the file type

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

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.