php-questions about checking file type functions

Source: Internet
Author: User
Keywords Php
Php

 /** * Check File type * * @access public * @param string filename * @param string Realna Me real file name * @param string limit_ext_types allowed file types * @return string */function Check_file_typ  E ($filename, $realname = ', $limit _ext_types = ') {if ($realname) {$extname = Strtolower (substr ($realname, Strrpos ($realname, '. ') + 1);//Gets the file suffix name} else {$extname = Strtolower (substr ($filename, Strrpos ($filenam E, '. ')    + 1)); if ($limit _ext_types && stristr ($limit _ext_types, ' | ' $extname. '|')    = = = False)//??    {return ';    } $str = $format = ";    $file = @fopen ($filename, ' RB ');    if ($file) {$str = @fread ($file, 0x400);//Read the first 1024 bytes @fclose ($file); } else {if (Stristr ($filename, root_path) = = = False) {if ($extname = = ' jpg ' | | $extname = = ' JPEG ' | | $extname = = ' gif ' | | $extname = = ' png ' | |             $extname = = ' Doc ' | |   $extname = = ' xls ' | | $extname = = ' txt ' | | $extname = = ' Zip ' | | $extname = = ' rar ' | |                $extname = = ' ppt ' | | $extname = = ' pdf ' | | $extname = = ' RM ' | | $extname = = ' Mid ' | | $extname = = ' wav ' | |                $extname = = ' bmp ' | | $extname = = ' swf ' | | $extname = = ' CHM ' | | $extname = = ' SQL ' | | $extname = = ' Cert ' | |                 $extname = = ' Pptx ' | | $extname = = ' xlsx ' | |            $extname = = ' docx ') {$format = $extname;        }} else {return '; }} if ($format = = ' && strlen ($STR) >= 2) {if (substr ($str, 0, 4) = = ' MThd ' && $ext        Name! = ' 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) = = ' GIF8 ' && $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) = = ' CWS ' | | 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, 0x200,4) = = "\xec\xa5\xc1\x00" | | $extname = = ' Doc ') {$format = ' doc '            ;             } elseif (substr ($str, 0x200,2) = = "\x09\x08" | | $extname = = ' xls ') {$format = ' xls ';  } elseif (substr ($str, 0x200,4) = = "\xfd\xff\xff\xff" | | $extname = = ' ppt ') {$format            = ' ppt '; }} elseif (substr ($str, 0, 4) == "pk\x03\x04") {if (substr ($str, 0x200,4) = = "\xec\xa5\xc1\x00" | | $extname = = ' docx ') {            $format = ' docx '; } elseif (substr ($str, 0x200,2) = = "\x09\x08" | | $extname = = ' xlsx ') {$format = ' xlsx            ';            } elseif (substr ($str, 0x200,4) = = "\xfd\xff\xff\xff" | | $extname = = ' pptx ') {$format = ' pptx ';            }else {$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 '; }} if ($limit _ext_types && stristr ($limit _ext_types, ' | ' $format. '|')    = = = False) {$format = '; } return $format;}

The entire function is used to detect the file type, where

 elseif (substr($str ,0, 3) == "\xFF\xD8\xFF")        {            $format = 'jpg';        }                 elseif (substr($str ,0, 8) == "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")        {            $format = 'png';        }                 elseif (substr($str ,0, 4) == "\xD0\xCF\x11\xE0")                   if (substr($str,0x200,4) == "\xEC\xA5\xC1\x00" || $extname == 'doc')            {                $format = 'doc';            }            elseif (substr($str,0x200,2) == "\x09\x08" || $extname == 'xls')            {                $format = 'xls';            } elseif (substr($str,0x200,4) == "\xFD\xFF\xFF\xFF" || $extname == 'ppt')            {                $format = 'ppt';            }

What are the principles of these statements? And in hexadecimal for everything?

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