Check the file type based on the file header

Source: Internet
Author: User
Tags fread unpack

File signatures are generally in the file header. If you view the file in hexadecimal mode, you can see some signature information of the file. For example, if you use uestudio to view ZIP files in hexadecimal format, the file content header contains hexadecimal information such as 50 4B 03 04. In the same way, the JPG file contains hexadecimal information such as FF D8 FF E0 XX 4A 46. In fact, the hexadecimal format represents some special notes.

How does PHP verify the file type? Let's look at a simple method:

Function checkfiletype ($ filename) {$ file = fopen ($ filename, "rb"); $ bin = fread ($ file, 2 ); // read-only 2-byte fclose ($ file); the old brand entertainment city // C is an unsigned integer, and all found on the Internet are C, which is a signed integer, in this case, a negative number is generated to determine whether it is normal. $ strinfo = @ unpack ("c2chars", $ bin); $ typecode = intval ($ strinfo ['chars1']. $ strinfo ['chars2 ']); $ filetype = ''; Switch ($ typecode) {Case '000000': return $ typecode. ':'. 'jpg '; break; Case '000000': return $ typecode. ':'. 'gif'; break; Case '000000': return $ typecode. ':'. 'png '; break; Case '000000': return $ typecode. ':'. 'bmp '; break; Case '000000': return $ typecode. ':'. 'exe '; break; Case '000000': return $ typecode. ':'. 'midi '; break; Case '000000': return $ typecode. ':'. 'rar '; break; default: return $ typecode. ':'. 'unknon'; break;} // return $ typecode;} $ file_name = '11.doc '; echo checkfiletype ($ file_name );

The following provides the implementation of a class:

/* Get the file type through the file name ** @ author chengmo QQ: 8292669 ** @ copyright http://www.cnblogs.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 $ fi Lename 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 of different file types, different header information. Fclose ($ file); $ typelist = self: $ _ typelist; foreach ($ typelist as $ v) {$ blen = strlen (pack ("H *", $ V [0]); // get the number of bytes marked in the file header $ TBIN = substr ($ bin, 0, intval ($ blen )); /// compare the object 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 (Array ('key ', value )...) */Public Function gettypelist () {return array (Array ("ffd8ff E1 "," 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 ("struct ", "eml"), array ("cfad12fec5fd746f", "DBX"), array ("2142444e", "Pst"), array ("d0cf11e0", "XLS/Doc "), array ("5374616e64617213204a", "MD B "), 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) ;}}$ filename = "22.jpg"; echo $ filename," T ", cfiletypecheck: getfiletype ($ filename ), "RN"; $ filename = "11.doc"; echo $ filename," T ", cfiletypecheck: getfiletype ($ filename)," RN ";

You can also perform the following checks:

$ Filename = '22.jpg '; $ extname = strtolower (substr ($ filename, strrpos ($ filename ,'. ') + 1); echo $ extname. '<br/>'; $ file = @ fopen ($ filename, 'rb'); if ($ file) {$ STR = @ fread ($ file, 0x400); // read the first 1024 bytes of ECHO substr ($ STR, 0, 4); @ fclose ($ file);} If (substr ($ STR, 0, 4) = 'mthd' & $ extname! = 'Txt ') {$ format = 'mid';} elseif (substr ($ STR, 0, 4) = 'riff '& $ extname = 'wav ') {$ format = 'wav ';} elseif (substr ($ STR, 0, 3) = "/xFF/xd8/xFF") {$ format = 'jpg ';} elseif (substr ($ STR, 0, 4) = 'gif 8' & $ extname! = 'Txt ') {$ format = 'gif';} elseif (substr ($ STR, 0, 8) = "/x89/x50/x4e/x47/x0d/x0a/x1a/x0a") {$ format = 'png ';} elseif (substr ($ STR, 0, 2) = 'bm '& $ extname! = 'Txt ') {$ format = 'bmp';} elseif (substr ($ STR, 0, 3) = 'done' | substr ($ STR, 0, 3) = 'fws ') & $ extname! = 'Txt ') {$ format = 'swf';} elseif (substr ($ STR, 0, 4) = "/xd0/xcf/X11/xe0 ") {// d0cf11e = docfile = Microsoft Office document if (substr ($ STR, 0 x, 4) = "/xec/xa5/xc1/x00" | $ extname = 'Doc') {$ format = 'Doc';} elseif (substr ($ STR, 0x200,2) = "/x09/x08" | $ extname = 'xls ') {$ format = 'xls';} elseif (substr ($ STR, 0 x, 4) = "/XFD/xFF" | $ extname = 'ppt ') {$ format = 'ppt ';} Elseif (substr ($ STR, 0, 4) = "PK/x03/x04") {$ format = 'zip';} elseif (substr ($ STR, 0, 4) = 'rar! '& $ Extname! = 'Txt ') {$ format = 'rar';} elseif (substr ($ STR, 0, 4) = "/x25pdf ") {$ format = 'pdf ';} elseif (substr ($ STR, 0, 3) = "/x30/x82/x0a") {$ format = 'cert ';} elseif (substr ($ STR, 0, 4) = 'itsf '& $ extname! = 'Txt ') {$ format = 'chm';} elseif (substr ($ STR, 0, 4) = "/x2ermf ") {$ format = 'rm ';} elseif ($ extname =' SQL ') {$ format =' SQL ';} elseif ($ extname = 'txt ') {$ format = 'txt ';} echo $ format;

Check the file type based on the file header

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.