PHP detection file type (based on file header information) _php tutorial

Source: Internet
Author: User
The article provides a PHP detection file type (according to the file header information) Oh, he can according to the user post file header information to the exact file type.

This article provides a PHP tutorial to detect the file type (based on the header information of the file) Oh, he can according to the user post file header information to the exact file type.

/* 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", "PS Tutorial D"),
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);
}
}

http://www.bkjia.com/PHPjc/444834.html www.bkjia.com true http://www.bkjia.com/PHPjc/444834.html techarticle The article provides a PHP detection file type (according to the file header information) Oh, he can according to the user post file header information to the exact file type. The article provides a PHP tutorial detection text ...

  • Related Article

    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.