The solution for PHP to determine the type of uploaded files.
Share with everyone the method for PHP to determine the type of uploaded files. Let’s learn together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | function checkTitle( $filename ){
$file = fopen ( $filename , "rb" );
$bin = fread ( $file , 2);
fclose( $file );
$strInfo =@unpack( "c2chars" , $bin );
$typeCode = intval ( $strInfo [ 'chars1' ]. $strInfo [ 'chars2' ]);
$fileType = '' ;
switch ( $typeCode ){
case 7790:
$fileType = 'exe' ;
break ;
case 7784:
$fileType = 'midi' ;
break ;
case 8297:
$fileType = 'rar' ;
break ;
case 255216:
$fileType = 'jpg' ;
break ;
case 7173:
$fileType = 'gif' ;
break ;
case 6677:
$fileType = 'bmp' ;
break ;
case 13780:
$fileType = 'png' ;
break ;
default :
$fileType = 'unknown' . $typeCode ;
break ;
}
if ( $strInfo [ 'chars1' ]== '-1' && $strInfo [ 'chars2' ]== '-40' ){
return 'jpg' ;
}
if ( $strInfo [ 'chars1' ]== '-119' && $strInfo [ 'chars2' ]== '80' ){
return 'png' ;
}
return $fileType ;
}
|
Copy after login
I hope this article will help everyone learn PHP programming.
http://www.bkjia.com/PHPjc/1063239.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1063239.htmlTechArticleThe solution for PHP to determine the type of uploaded files. Share with everyone the method for determining the type of uploaded files in PHP. Let’s learn together. . /** * Read the first few bytes of the file to determine the file type...