用PHP解决万恶的BOM_PHP教程
$path = dirname(__FILE__);
scandDir($path);
function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;
if (is_dir($filename)) return;
$fd = fopen($filename, "rb");
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);
if (strlen($headers)
if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, "\n";
$content = file_get_contents($filename);
if (!$content) {
exit("file get contents failed");
}
$len = strlen($content);
$content = substr($content, 3);
if ($content === false && $len!=3) {
exit("sbustr failed");
} else if ($content === false && $len==3){
$content = "";
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit("file put contents failed");
}
echo "successed file ", $filename, "\n";
}
}
function scandDir($path)
{
$d = dir($path);
if (!$d) exit("dir failed");
while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$filename = $path . DIRECTORY_SEPARATOR . $entry;
// echo $filename, "\n";
if (is_dir($filename)) {
scandDir($filename);
} else {
bomtoutf8($filename);
}
}
}
$path = dirname(__FILE__);
scandDir($path);
function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;
if (is_dir($filename)) return;
$fd = fopen($filename, "rb");
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);
if (strlen($headers)
if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, "\n";
$content = file_get_contents($filename);
if (!$content) {
exit("file get contents failed");
}
$len = strlen($content);
$content = substr($content, 3);
if ($content === false && $len!=3) {
exit("sbustr failed");
} else if ($content === false && $len==3){
$content = "";
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit("file put contents failed");
}
echo "successed file ", $filename, "\n";
}
}
function scandDir($path)
{
$d = dir($path);
if (!$d) exit("dir failed");
while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$filename = $path . DIRECTORY_SEPARATOR . $entry;
// echo $filename, "\n";
if (is_dir($filename)) {
scandDir($filename);
} else {
bomtoutf8($filename);
}
}
}
友情提示:代码是批量处理目录下(包括子目录)的所有带BOM文件,使用前需备份
推荐阅读
-
想升级下php的版本,目前用哪个版本比较好解决思路
-
php用header()实现文件下载,下载的文件提示被破坏不能打开的解决办法
-
php用header()实现文件下载,下载的文件提示被破坏不能打开的解决办法
-
destoon整合ucenter后注册页面不跳转的解决方法_PHP教程
-
用PHP制作的意见反馈表_PHP教程
-
destoon后台网站设置变成空白的解决方法_PHP教程
-
如何解决PHP里大量数据循环时内存耗尽的问题_PHP教程
-
用PHP向数据库中实现简单的增删改查(纯代码,待完善),php增删_PHP教程
-
类似百度文库pdf2swf+flexpaper解决pdf在线阅读的效果,pdf2swfflexpaper_PHP教程
-
php输出xml必须header的解决方法,phpxmlheader_PHP教程