如下代码为PHP方式去除当前目录及子目录所有文件BOM信息的代码,新建文件,将其放倒根目录下,然后浏览器访问即可。
<?php<BR>if (isset($_GET['dir'])) { //设置文件目录 <BR> $basedir = $_GET['dir'];<BR>} else {<BR> $basedir = '.';<BR>}</P><P>$auto = 1;<BR>checkdir($basedir);</P><P>function checkdir($basedir)<BR>{<BR> if ($dh = opendir($basedir)) {<BR> while (($file = readdir($dh)) !== false) {<BR> if ($file != '.' && $file != '..') {<BR> if (!is_dir($basedir . "/" . $file)) {<BR> echo "filename: $basedir/$file " . checkBOM("$basedir/$file") . " <br>";<BR> } else {<BR> $dirname = $basedir .<mark>@本文来源gaodaimacom搞#代%码@网-</mark><strong>搞代gaodaima码</strong> "/" . $file;<BR> checkdir($dirname);<BR> }<BR> }<BR> }<BR> closedir($dh);<BR> }<BR>}<BR>function checkBOM($filename)<BR>{<BR> global $auto;<BR> $contents = file_get_contents($filename);<BR> $charset[1] = substr($contents, 0, 1);<BR> $charset[2] = substr($contents, 1, 1);<BR> $charset[3] = substr($contents, 2, 1);<BR> if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {<BR> if ($auto == 1) {<BR> $rest = substr($contents, 3);<BR> rewrite($filename, $rest);<BR> return ("<font color="red">BOM found, automatically removed.</font>");<BR> } else {<BR> return ("<font color="red">BOM found.</font>");<BR> }<BR> } else<BR> return ("BOM Not Found.");<BR>}</P><P>function rewrite($filename, $data)<BR>{<BR> $filenum = fopen($filename, "w");<BR> flock($filenum, LOCK_EX);<BR> fwrite($filenum, $data);<BR> fclose($filenum);<BR>}<BR>?><BR>