[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) < 3) return;
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) < 3) return;
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);
}
}
}
Friendly tip: Code is the batch processing directory (including subdirectories) of all the with BOM files, before use to backup
http://www.bkjia.com/PHPjc/477261.html www.bkjia.com true http://www.bkjia.com/PHPjc/477261.html techarticle php $path = dirname (__file__), Scanddir ($path), function Bomtoutf8 ($filename) {$ef = 239; $bb = 187; $BF = 191 ; if (Is_dir ($filename)) return; $FD = fopen ($filename, RB); I ...