[Php]
<? 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 );
}
}
}
<? 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 );
}
}
}
Tip: the code is used to process all BOM files in the directory (including subdirectories) in batches. You must back up the files before use.