Php detects common code classes (such as zip and rar) of file types through the file header)

Source: Internet
Author: User
When using a web application, you can use the web extension to determine the type of the stored file. this is often used. Sometimes this is not perfect. Some people may store some files, but they modify the extension to make it within our file type. It cannot be displayed during actual access (because the extension does not match the file content ). The following php class may help us.
I. php detection
First, the ing between the above file header and file type comes from the internet. if you have a new file to be checked, you only need to add the ING. If you need to know the file header information, you can use the tool winhex to open the standard file search. For example:
Code:

The code is as follows:


/* Obtain the file type by file name *
* @ Author chengmo *
* @ Copyright cnblog.com/chengmo 2010-10-17
* @ Version 0.1
* $ Filename = "d:/1.png"; echo cFileTypeCheck: getFileType ($ filename); print: png
*/
Class cFileTypeCheck
{
Private static $ _ TypeList = array ();
Private static $ CheckClass = null;
Private function _ construct ($ filename)
{
Self: $ _ TypeList = $ this-> getTypeList ();
}
/**
* Process the file type ing table *
*
* @ Param string $ filename File type
* @ Return string file type. the returned value is 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 for different file types and different header information.
Fclose ($ file );
$ Typelist = self ::$ _ TypeList;
Foreach ($ typelist as $ v)
{
$ Blen = strlen (pack ("H *", $ v [0]); // get the number of bytes marked by the file header
$ Tbin = substr ($ bin, 0, intval ($ blen); // compare the file header length
If (strtolower ($ v [0]) = strtolower (array_shift (unpack ("H *", $ tbin ))))
{
Return $ v [1];
}
}
Return $ filetype;
}
/**
* Get the file header and file type ing table *
*
* @ Return array ('key', value )...)
*/
Public function getTypeList ()
{
Return array ("FFD8FFE1", "jpg "),
Array ("89504E47", "png "),
Array ("47494638", "gif "),
Array ("49347a00", "tif "),
Array ("mongod", "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 ("5374616e64617213204a", "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 ("2e00001fd", "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 header bytecode:

We can see that the header of the png file is 4 bytes (the number of bytes marked by the header needs to be determined by yourself), corresponding to: 89504E47
If you are not familiar with the pack unpack of php, you can view:
Php park, unpark, ord function usage (binary stream interface application instance)

Call instance:

The code is 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

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.