PHP detects file types by file header generic code class (Zip,rar, etc.)

Source: Internet
Author: User
Tags unpack
Sometimes we don't do it well. Some people may have saved some files, but he has changed the extension so that it is within our file type. Cannot be displayed when a single actual visit (because the extension does not match the contents of the file). The following PHP class may be helpful to us.
First, the PHP detection class
First of all, the above file header and file type mapping relationship from the Internet, if you have a new file to check, only need to add the mapping. If you need to know the file header information, you can open the standard file lookup by using the tool: Winhex. Such as:
Code:

Copy the Code code as follows:


/* Get the file type by filename *
* @author chengmo*
* @copyright Cnblog.com/chengmo 2010-10-17
* @version 0.1
* $filename = "d:/1.png"; Echo Cfiletypecheck::getfiletype ($filename); Printing: PNG
*/
Class Cfiletypecheck
{
private static $_typelist=array ();
private static $CheckClass =null;
Private function __construct ($filename)
{
self::$_typelist= $this->gettypelist ();
}
/**
* Process file Type mapping Relationship table *
*
* @param string $filename file type
* @return string file type, not found returned: other
*/
Private Function _getfiletype ($filename)
{
$filetype = "other";
if (!file_exists ($filename)) throw new Exception ("No found file!");
$file = @fopen ($filename, "RB");
if (! $file) throw new Exception ("File refuse!");
$bin = Fread ($file, 15); Read-only 15 bytes each different file type, header information is not the same.
Fclose ($file);
$typelist =self::$_typelist;
foreach ($typelist as $v)
{
$blen =strlen (Pack ("h*", $v [0]); Gets the number of file header tokens in bytes
$tbin =substr ($bin, 0,intval ($blen)); Need to compare file header lengths
if (Strtolower ($v [0]) ==strtolower (Array_shift (Unpack ("h*", $tbin))))
{
return $v [1];
}
}
return $filetype;
}
/**
* Get file header and File type mapping table *
*
* @return Array Array (' key ', value) ...)
*/
Public Function Gettypelist ()
{
return Array (Array ("Ffd8ffe1", "jpg"),
Array ("89504E47", "PNG"),
Array ("47494638", "gif"),
Array ("49492a00", "TIF"),
Array ("424D", "BMP"),
Array ("41433130", "DWG"),
Array ("38425053", "PSD"),
Array ("7b5c727466", "RTF"),
Array ("3C3F786D6C", "xml"),
Array ("68746d6c3e", "html"),
Array ("44656c69766572792d646174", "eml"),
Array ("cfad12fec5fd746f", "dbx"),
Array ("2142444E", "PST"),
Array ("D0cf11e0", "Xls/doc"),
Array ("5374616e64617264204a", "MDB"),
Array ("FF575043", "WPD"),
Array ("252150532d41646f6265", "Eps/ps"),
Array ("255044462d312e", "PDF"),
Array ("E3828596", "PWL"),
Array ("504b0304", "zip"),
Array ("52617221", "rar"),
Array ("57415645", "wav"),
Array ("41564920", "avi"),
Array ("2E7261FD", "Ram"),
Array ("2e524d46", "rm"),
Array ("000001BA", "mpg"),
Array ("000001b3", "mpg"),
Array ("6d6f6f76", "mov"),
Array ("3026b2758e66cf11", "ASF"),
Array ("4d546864", "mid"));
}
public static function Getfiletype ($filename)
{
if (!self:: $CheckClass) Self:: $CheckClass =new self ($filename);
$class =self:: $CheckClass;
Return $class->_getfiletype ($filename);
}
}


How to get the first byte code:

Can see: PNG file, the header is 4 bytes (the header tag is how many bytes need to check the relevant data to determine), corresponds to: 89504E47
If you are not familiar with PHP's pack unpack, you can view:
PHP Park, Unpark, Ord function usage (binary stream interface application example)
Invoke instance:

Copy the Code code as follows:


$filename = "D:/1.png";
echo $filename, "\ T", Cfiletypecheck::getfiletype ($filename), "\ r \ n";
$filename = "D:/test.doc";
echo $filename, "\ T", Cfiletypecheck::getfiletype ($filename), "\ r \ n";
D:/1.png png
D:/test.doc Xls/doc

The above describes PHP through the file header detection file type Generic code class (Zip,rar, etc., including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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