Batch removal of PHP code from bom in PHP file

  • 2020-05-16 06:29:38
  • OfStack

To remove BOM, put the attached tool.php file into the target directory and then access tool.php in your browser!
 
<?php 
// This file is used for quick tests UTF8 Is the coded file added BOM And can be removed automatically  
$basedir="."; // Modify the directory to be detected for this behavior, and the dot represents the current directory  
$auto=1; // Whether to automatically remove found BOM Information. 1 As is, 0 For ever.  
// The following is unchanged  
if ($dh = opendir($basedir)) { 
while (($file = readdir($dh)) !== false) { 
if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) 
echo "filename: $file ".checkBOM("$basedir/$file")." <br>"; 
} 
closedir($dh); 
} 
function checkBOM ($filename) { 
global $auto; 
$contents=file_get_contents($filename); 
$charset[1]=substr($contents, 0, 1); 
$charset[2]=substr($contents, 1, 1); 
$charset[3]=substr($contents, 2, 1); 
if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191) { 
if ($auto==1) { 
$rest=substr($contents, 3); 
rewrite ($filename, $rest); 
return ("<font color=red>BOM found, automatically removed.</font>"); 
} else { 
return ("<font color=red>BOM found.</font>"); 
} 
}else 
return ("BOM Not Found."); 
} 
function rewrite ($filename, $data) { 
$filenum=fopen($filename,"w"); 
flock($filenum,LOCK_EX); 
fwrite($filenum,$data); 
fclose($filenum); 
} 
?> 

PHP batch removes the bom code from the PHP file
 
<?php 
if (isset($_GET['dir'])){ // Set file directory  
$basedir=$_GET['dir']; 
}else{ 
$basedir = '.'; 
} 
$auto = 1; 
checkdir($basedir); 
function checkdir($basedir){ 
if ($dh = opendir($basedir)) { 
while (($file = readdir($dh)) !== false) { 
if ($file != '.' && $file != '..'){ 
if (!is_dir($basedir."/".$file)) { 
echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>"; 
}else{ 
$dirname = $basedir."/".$file; 
checkdir($dirname); 
} 
} 
} 
closedir($dh); 
} 
} 
function checkBOM ($filename) { 
global $auto; 
$contents = file_get_contents($filename); 
$charset[1] = substr($contents, 0, 1); 
$charset[2] = substr($contents, 1, 1); 
$charset[3] = substr($contents, 2, 1); 
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { 
if ($auto == 1) { 
$rest = substr($contents, 3); 
rewrite ($filename, $rest); 
return ("<font color=red>BOM found, automatically removed._<a href=http://www.joyphper.net>http://www.joyphper.net</a></font>"); 
} else { 
return ("<font color=red>BOM found.</font>"); 
} 
} 
else return ("BOM Not Found."); 
} 
function rewrite ($filename, $data) { 
$filenum = fopen($filename, "w"); 
flock($filenum, LOCK_EX); 
fwrite($filenum, $data); 
fclose($filenum); 
} 
?> 

Related articles: