PHP判断上传文件类型的解决办法

yipeiwu_com6年前PHP代码库

分享给大家php判断上传文件类型的方法,大家一起学习学习。

/** 
* 读取文件前几个字节 判断文件类型 
* @return String 
*/ 
function checkTitle($filename){ 
  $file=fopen($filename, "rb"); 
  $bin=fread($file, 2); //只读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; 
  } 
  //Fix 
  if($strInfo['chars1']=='-1' && $strInfo['chars2']=='-40'){ 
    return 'jpg'; 
  } 
  if($strInfo['chars1']=='-119' && $strInfo['chars2']=='80'){ 
    return 'png'; 
  } 
  return $fileType; 
} 

希望通过本文对大家学习php程序设计有所帮助。

相关文章

Could not load type System.ServiceModel.Activation.HttpModule解决办法

Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.Service...

golang 调用 php7详解及实例

执行php文件 func Test_exec(t *testing.T) { engine.Initialize() ctx := &engine.Context{...

PHP常用技巧汇总

本文为大家分享了多个php常用技巧,供大家参考,具体内容如下 1、PHP文件读取函式 //文件读取函式 function PHP_Read($file_name) { $fd=fo...

PHP导入导出Excel代码

一.导入   导入需要使用能读取Excel的组件,网上也有比较好的组件,这里分享我使用的:下载  提取码:vxyn。(注意两个文件有引用关系) <?php /...

php使用simple_html_dom解析HTML示例

本文实例讲述了php使用simple_html_dom解析HTML的方法。分享给大家供大家参考,具体如下: 今天写了两个爬虫, 一个使用Python, 一个使用PHP, 说实在, 两个实...