您现在的位置是:首页 >

为什么二进制比文本文件小 PHP取二进制文件头快速判断文件类型

火烧 2023-04-05 16:35:36 1042
PHP取二进制文件头快速判断文件类型   一般我们都是按照文件扩展名来判断文件类型 但是这个很不靠谱 轻易就通过修改扩展名来躲避了 一般必须要读取文件信息来识别    lt ? h   $file =

PHP取二进制文件头快速判断文件类型  

  一般我们都是按照文件扩展名来判断文件类型 但是这个很不靠谱 轻易就通过修改扩展名来躲避了 一般必须要读取文件信息来识别

  <?php

  $files = array( /test jpg test png )

  $fileTypes = array(

       => exe

       => midi

       => zip

       => rar

     => jpg

       => gif

       => bmp

      => png

  )

  foreach($files as $file) {

为什么二进制比文本文件小 PHP取二进制文件头快速判断文件类型

  $fp = fopen($file rb )

  $bin = fread($fp ) // 只读头两个字节

  fclose($fp)

  $strInfo = @unpack( C chars $bin)

  $typeCode = intval($strInfo[ chars ] $strInfo[ chars ])

  $fileType = isset($fileTypes[$typeCode]) ? $fileTypes[$typeCode] : unknown ;

  echo $file type : <b> $fileType </b> code : <b> $fileType </b><br /> ;

lishixinzhi/Article/program/PHP/201311/20814  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码