当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP批量检测并去除文件BOM头信息代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-06-20 10:01:55 浏览: 评论:0 

因为文件头信息输出BOM头信息,有时会对程序的执行结果造成影响,那么此时即应对这些文件的BOM信息进行去除,如下代码为PHP方式去除当前目录及子目录所有文件BOM信息的代码,新建文件,将其放倒根目录下,然后浏览器访问即可.

例子:将以上代码保存为后缀为php的文件放到需要去除bom的文件目录里面,然后运行该php文件,将会对该目录以及该目录所有的子目录下的文件进行bom检查并去除bom,代码如下:

  1. <?php 
  2. if (isset($_GET['dir'])) { //设置文件目录   
  3.     $basedir = $_GET['dir']; 
  4. } else { 
  5.     $basedir = '.'; 
  6. } 
  7.  
  8. $auto = 1; 
  9. checkdir($basedir); 
  10.  
  11. function checkdir($basedir) 
  12. { 
  13.     if ($dh = opendir($basedir)) { 
  14.         while (($file = readdir($dh)) !== false) { 
  15.             if ($file != '.' && $file != '..') { 
  16.                 if (!is_dir($basedir . "/" . $file)) { 
  17.                     echo "filename: $basedir/$file " . checkBOM("$basedir/$file") . " <br>"; 
  18.                 } else { 
  19.                     $dirname = $basedir . "/" . $file; 
  20.                     checkdir($dirname); 
  21.                 } 
  22.             } 
  23.         } 
  24.         closedir($dh); 
  25.     } 
  26. } 
  27. function checkBOM($filename) 
  28. { 
  29.     global $auto; 
  30.     $contents   = file_get_contents($filename); 
  31.     $charset[1] = substr($contents, 0, 1); 
  32.     $charset[2] = substr($contents, 1, 1); 
  33.     $charset[3] = substr($contents, 2, 1); 
  34.     if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { 
  35.         if ($auto == 1) { 
  36.             $rest = substr($contents, 3); 
  37.             rewrite($filename, $rest); 
  38.             return ("<font color="red">BOM found, automatically removed._<a href="http://www.phpfensi.com">http://www.phpfensi.com</a></font>"); 
  39.         } else { 
  40.             return ("<font color="red">BOM found.</font>"); 
  41.         } 
  42.     } else 
  43.         return ("BOM Not Found."); 
  44. } 
  45.  
  46. function rewrite($filename, $data) 
  47. { 
  48.     $filenum = fopen($filename, "w"); 
  49.     flock($filenum, LOCK_EX); 
  50.     fwrite($filenum, $data); 
  51.     fclose($filenum); 
  52. } 
  53. ?> 

例子二,代码如下:

  1. <?php 
  2. header('content-Type: text/html; charset=utf-8'); 
  3. $auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/ 
  4. $basedir='.'; 
  5. $loop=true;//www.phpfensi.com echo '当前查找的目录为:'.$basedir.'当前的设置是:'; 
  6. echo '(1)',$loop?'检查当前目录以及当前目录的子目录':'只针对当前目录进行检测'; 
  7. echo '(2)',$auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />'; 
  8.  
  9. checkdir($basedir,$loop); 
  10. function checkdir($basedir='',$loop=true){ 
  11.  $basedir=emptyempty($basedir)?'.':$basedir; 
  12.  if($dh=opendir($basedir)){ 
  13.   while (($file=readdir($dh))!==false){ 
  14.    if($file!='.'&&$file!='..'){ 
  15.     if(!is_dir($basedir.'/'.$file)){ 
  16.      echo '文件: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).' <br>'; 
  17.     }else{ 
  18.      if(!$loop) continue; 
  19.      $dirname=$basedir.'/'.$file; 
  20.      checkdir($dirname); 
  21.     } 
  22.    } 
  23.   } 
  24.   closedir($dh); 
  25.  } 
  26. } 
  27. function checkBOM($filename){ 
  28.  global $auto; 
  29.  $contents=file_get_contents($filename); 
  30.  $charset[1]=substr($contents,0,1); 
  31.  $charset[2]=substr($contents,1,1); 
  32.  $charset[3]=substr($contents,2,1); 
  33.  if(ord($charset[1])==239&&ord($charset[2])==187&&ord($charset[3])==191){ 
  34.   if($auto==1){ 
  35.    $rest=substr($contents,3); 
  36.    rewrite($filename,$rest); 
  37.    return (' <font color=red>找到BOM并已自动去除</font>'); 
  38.   }else{ 
  39.    return (' <font color=red>找到BOM</font>'); 
  40.   } 
  41.  }else{ 
  42.   return (' 没有找到BOM'); 
  43.  } 
  44. } 
  45. function rewrite($filename,$data){ 
  46.  $filenum=fopen($filename,'w'); 
  47.  flock($filenum,LOCK_EX); 
  48.  fwrite($filenum,$data); 
  49.  fclose($filenum); 
  50. } 

Tags: PHP批量检测 去除文件BOM信息

分享到: