Home > Backend Development > PHP Tutorial > Powerful PHP file type check_PHP tutorial

Powerful PHP file type check_PHP tutorial

WBOY
Release: 2016-07-13 10:55:17
Original
967 people have browsed it

A powerful file type detection function, it can determine the type of file you upload. You cannot install this. Friends in need can refer to it.

The code is as follows Copy code
**
*
*
* @access public
* @param string filename filename
* @param string limit_ext_types Allowed file types, types surrounded by | such as: |gif|txt|
* @return string
*/
function check_file_type($filename, $limit_ext_types = ''){
$extname = strtolower(substr($filename, strrpos($filename, '.') + 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{
$format=$extname;
}

if ($format == '' && strlen($str) >= 2 ){
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) == "xFFxD8xFF"){
$format = 'jpg';
}
elseif (substr($str ,0, 4) == 'GIF8' && $extname != 'txt'){
$format = 'gif';
}
elseif (substr($str ,0, 8) == "x89x50x4Ex47x0Dx0Ax1Ax0A"){
$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) == "xD0xCFx11xE0"){ // D0CF11E == DOCFILE == Microsoft Office Document
if (substr($str,0x200,4) == "xECxA5xC1x00"
|| $extname == 'doc'){
$format = 'doc';
}
elseif (substr($str,0x200,2) == "x09x08" || $extname == 'xls'){
$format = 'xls';
}
elseif (substr($str,0x200,4) == "xFDxFFxFFxFF"
|| $extname == 'ppt'){
$format = 'ppt';
}
}
elseif (substr($str ,0, 4) == "PKx03x04"){
$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) == "x30x82x0A"){
$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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632260.htmlTechArticleA powerful file type detection function, it can determine what type of file you upload. There is nothing you can do for this I installed it, friends in need can refer to it. The code is as follows...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template