用PHP解决万恶的BOM
$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登录中的防止sql注入方法分析
下一篇: 受的许多模拟的http请求,求解决方法。
推荐阅读
-
用PHP解决万恶的BOM
-
用laravel构建的app,登录之后,再把app进程杀死重新进入,还要重新登录,请问如何解决
-
php网站提示“您不具备查...资源进行了配置”的解决方法
-
问:一个天才的有关问题可以用php得到msn中好友的信息吗
-
PHP实例:用PHP编写的网上调查投票系统
-
PHP MVC中的不解解决办法
-
“无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装”最快的解决办法
-
PHP调用存储过程返回值不一致问题的解决方法分析,存储过程不一致_PHP教程
-
ajax传编辑器的内容到php,php端代码不完整,该怎么解决
-
解决php snoopy远程获取数据的稳定性有关问题