zoukankan      html  css  js  c++  java
  • 检测php文件是否有bom头

    <?
    02    //此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除
    03    //By Bob Shen
    04    
    05    $basedir="."; //修改此行为需要检测的目录,点表示当前目录
    06    $auto=1; //是否自动移除发现的BOM信息。1为是,0为否。
    07    
    08    //以下不用改动
    09    
    10    if ($dh = opendir($basedir)) {
    11    while (($file = readdir($dh)) !== false) {
    12    if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file")." <br>";
    13    }
    14    closedir($dh);
    15    }
    16    
    17    function checkBOM ($filename) {
    18    global $auto;
    19    $contents=file_get_contents($filename);
    20    $charset[1]=substr($contents, 0, 1);
    21    $charset[2]=substr($contents, 1, 1);
    22    $charset[3]=substr($contents, 2, 1);
    23    if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191) {
    24    if ($auto==1) {
    25    $rest=substr($contents, 3);
    26    rewrite ($filename, $rest);
    27    return ("<font color=red>BOM found, automatically removed.</font>");
    28    } else {
    29    return ("<font color=red>BOM found.</font>");
    30    }
    31    }
    32    else return ("BOM Not Found.");
    33    }
    34    
    35    function rewrite ($filename, $data) {
    36    $filenum=fopen($filename,"w");
    37    flock($filenum,LOCK_EX);
    38    fwrite($filenum,$data);
    39    fclose($filenum);
    40    }

  • 相关阅读:
    jquery水印插件:placeholder
    EF POWER TOOLS由数据库逆向CODE FIRST
    .NET重构(类型码的设计、重构方法)
    CodeUI Test:创建第一个CodeUI Test
    Windows 8 Store Apps
    ASP.NET MVC 使用MSBuild部署的几个注意事项
    c#中如何跨线程调用windows窗体控件
    Restful?
    Javascript的一种代码结构方式——插件式
    AOP编程
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2891547.html
Copyright © 2011-2022 走看看