Php obtains the actual file type (for users who have changed the file extension ),

Source: Internet
Author: User

Php obtains the actual file type (for users who have changed the file extension ),

 

/*** Get the real file extension * @ param string $ file path ***/function getFileType ($ file) {$ fp = fopen ($ file, "rb"); $ bin = fread ($ fp, 2); // read-only 2 bytes fclose ($ fp); $ str_info = @ unpack ("C2chars ", $ bin); $ type_code = intval ($ str_info ['chars1']. $ str_info ['chars2 ']); $ file_type = ''; switch ($ type_code) {case 7790: $ file_type = 'exe'; break; case 7784: $ file_type = 'midi '; break; case 8075: $ file_type = 'zip'; break; case 8297: $ file_type = 'rar'; break; case 255216: $ file_type = 'jpg '; break; case 7173: $ file_type = 'gif'; break; case 6677: $ file_type = 'bmp'; break; case 13780: $ file_type = 'png '; break; default: $ file_type = 'unknown'; break;} return $ file_type ;}

 

 

Related Article

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.