php清楚BOM头

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

 

 

转载于:https://my.oschina.net/mengdejun/blog/9427

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值