


Sometimes we do this imperfectly. Some people may save some files on their computer, but they change the extension so that they fall 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 be able to help us.
1. PHP detection class
First of all, let me explain that the above mapping relationship between file headers and file types comes from the Internet. If you have new files to check, you only need to add the mapping. If you need to know the file header information, you can open the standard file and search through the tool: winhex. For example:
Code:
Copy code The code is as follows:
/*Get the file type through the 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 file type mapping table*
*
* @param string $filename file type
* @return string file type, not found return: 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, the header information is different .
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)); ///Need to compare the length of the file header
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(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 header bytecode:

You can see : png file, the header is 4 bytes (you need to check the relevant information to determine how many bytes the header mark is), the corresponding is: 89504E47
If you are not very familiar with PHP's pack unpack, you can check:
php park, unpark, How to use ord function (binary stream interface application example)
Calling example:
Copy code The code is as follows:
$filename="d:/1.png";
echo $filename,"t", cFileTypeCheck::getFileType($filename),"rn";
$filename="d:/test.doc";
echo $filename,"t",cFileTypeCheck::getFileType($filename),"rn";
d :/1.png png
d:/test.doc xls/doc
The above introduces the general code class for PHP to detect file types through file headers (zip, rar, etc.), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

在PHP中,有许多强大的数组函数可以使数组的操作更加方便和快捷。当我们需要将两个数组拼成一个关联数组时,可以使用PHP的array_combine函数来实现这一操作。这个函数实际上是用来将一个数组的键作为另一个数组的值,合并成一个新的关联数组。接下来,我们将会讲解如何使用PHP中的array_combine函数将两个数组拼成关联数组。了解array_comb

JavaScript 函数提供两个接口实现与外界的交互,其中参数作为入口,接收外界信息;返回值作为出口,把运算结果反馈给外界。下面本篇文章带大家了解一下JavaScript函数返回值,浅析下return语句的用法,希望对大家有所帮助!

Vue3.2setup语法糖是在单文件组件(SFC)中使用组合式API的编译时语法糖解决Vue3.0中setup需要繁琐将声明的变量、函数以及import引入的内容通过return向外暴露,才能在使用的问题1.在使用中无需return声明的变量、函数以及import引入的内容,即可在使用语法糖//import引入的内容import{getToday}from'./utils'//变量constmsg='Hello!'//函数func

Python返回值return用法是当函数执行到return语句时,将立即停止执行,并将指定的值返回给调用函数的地方。详细用法:1、返回单个值;2、返回多个值;3、返回空值;4、提前结束函数的执行。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver CS6
Visual web development tools
