php批量去文件bom头

使用方法:将下列代码复制后新建PHP文件,如FixBOM.php,然后http://你的域名/FixBOM.php?dir=F:\JENA\PROGRAM\program,参数dir即需要检查的绝对路径。


< ?php // remove the utf-8 boms // by magicbug at gmail dot com // EDIT.JENA.20140115.0109 set_time_limit ( 0 ); ob_end_clean (); ob_implicit_flush ( 1 ); if (isset ( $_GET ['dir'] )) { // config the basedir $basedir = $_GET ['dir']; } else { $basedir = '.'; } // 自动修复 $autoFix = 1; // 忽略类型 $ignoreExt = array ( 'jpg', 'png', 'gif', 'flv', 'mp4', 'wmv', 'rar', 'zip', 'txt' ); // 忽略目录 $ingoreDir = array ( 'upload' ); checkdir ( $basedir ); echo (‘completed!’);
function checkdir($basedir) {
global $ignoreExt;
global $ingoreDir;
if (! ! $dh = opendir ( $basedir )) {
while ( ($file = readdir ( $dh )) !== false ) {
if ($file != '.' && $file != '..' && ! in_array ( $file, $ingoreDir )) {
$pathinfo = pathinfo ( $file );
$ext = strtolower ( $pathinfo ['extension'] );
if (! is_dir ( $basedir . "/" . $file ) && ! in_array ( $ext, $ignoreExt )) {
checkBOM ( "$basedir/$file" );
} else {
$dirname = $basedir . "/" . $file;
checkdir ( $dirname );
}
}
}
closedir ( $dh );
}
}
function checkBOM($filename) {
global $autoFix;
$contents = file_get_contents ( $filename, NULL, NULL, 0, 10 );
$charset [1] = substr ( $contents, 0, 1 );
$charset [2] = substr ( $contents, 1, 1 );
$charset [3] = substr ( $contents, 2, 1 );
if (ord ( $charset [1] ) == 239 && ord ( $charset [2] ) == 187 && ord ( $charset [3] ) == 191) {
if ($autoFix == 1) {
$contents = file_get_contents ( $filename );
$rest = substr ( $contents, 3 );
rewrite ( $filename, $rest );
echo (str_repeat ( "

", 200 ) . $filename . "--------" . ‘BOM found, And removed.’);
} else {
echo (str_repeat ( "

", 200 ) . $filename . "--------" . ‘BOM found.’);
}
}
}
function rewrite($filename, $data) {
$filenum = fopen ( $filename, "w" );
flock ( $filenum, LOCK_EX );
fwrite ( $filenum, $data );
fclose ( $filenum );
}
?>

Leave a Comment

Your email address will not be published.

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据