PHP implementation of file headers to determine the format of the method

Source: Internet
Author: User
This article mainly introduces the PHP implementation through the file header to determine the format of the method, involving PHP file reading and string parsing related skills, the need for friends can refer to the next

Specific as follows:

function Judgefile ($file, $form) {if (!empty ($file) &&!empty ($form)) {$filehead = fopen ($file, ' R '); $bin = Fread ( $filehead, 2); fclose ($filehead); $data = Unpack (' C2chars ', $bin); $type _code = Intval ($data [' chars1 ']. $data [' chars2 ']) Switch ($type _code) {case 7790: $fileType = ' exe ';  Case 7784: $fileType = ' midi ';  Case 8075: $fileType = ' zip ';  Case 8297: $fileType = ' rar ';  Case 255216: $fileType = ' jpg ';  Case 7173: $fileType = ' gif ';  Case 6677: $fileType = ' bmp ';  Case 13780: $fileType = ' png ';  Default: $fileType = ' unknown '; if (!is_array ($form)) {if ($fileType = = $form) {return true;} Else{return false;}} Else{if (In_array ($file, $form)) {return true;} Else{return FALSE;}}} Else{return false;}}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.