PHP Read file header to determine the file type implementation Code _php tutorial

Source: Internet
Author: User
Tags php read file
PHP code to read the file header to determine the type of file, support images, RAR, exe and other suffixes.
Case:
Copy CodeThe code is as follows:
The path to the picture can be used absolute path such as D:/upload/11.jpg
$file = fopen ($filename, "RB");
$bin = Fread ($file, 2); Read-only 2 bytes
Fclose ($file);
$strInfo = @unpack ("C2chars", $bin);
$typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']);
$fileType = ";
Switch ($typeCode) {
Case 7790: $fileType = ' exe '; Break
Case 7784: $fileType = ' midi '; Break
Case 8297: $fileType = ' rar '; Break
Case 255216: $fileType = ' jpg '; Break
Case 7173: $fileType = ' gif '; Break
Case 6677: $fileType = ' bmp '; Break
Case 13780: $fileType = ' png '; Break
Default:echo ' unknown ';
}
Echo ' This is a '. $fileType. ' File: '. $typeCode;

Case:
Copy CodeThe code is as follows:
?>
PHP also has a function to determine the file type under Linux
echo mime_content_type (' 11.gif '). "\ n";
echo mime_content_type (' 22.php ');
?>

http://www.bkjia.com/PHPjc/328079.html www.bkjia.com true http://www.bkjia.com/PHPjc/328079.html techarticle PHP code to read the file header to determine the type of file, support images, RAR, exe and other suffixes. Case: Copy code code as follows: PHP $filename = "11.jpg"; The path for the picture can be used ...

  • 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.