Home >Backend Development >PHP Tutorial >Use PHP to solve all evil BOM_PHP tutorial

Use PHP to solve all evil BOM_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:12:01886browse

[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 reminder: The code is all BOM files in the batch processing directory (including subdirectories), and needs to be backed up before use

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477261.htmlTechArticle[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); i...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn