Remember to back up the files before running the code to avoid failure problems.
Code 1:
The code is as follows: echo 'The current directory being searched is: '.$basedir.' The current setting is: '; checkdir($basedir);
header('content-Type: text/html; charset=utf-8');
if(isset($_GET['dir'])){ / /Set the file directory. If not set, it will automatically be set to the directory where the current file is located
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;/*Set to 1 to detect BOM and remove it, set to 0 to only detect BOM and not remove it*/
echo $auto?' Detect the BOM of the file and remove the BOM of the detected BOM file
': 'Only detect file BOM and do not perform BOM removal
';
function checkdir($basedir){
if($dh=opendir($basedir)){
while (($file=readdir($dh)) ! == false){
if ($file != '.' && $file != '..') > echo 'File: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).'
';
not not less echo 'File:'. basedir.'/'.$file;
🎜> }
}
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){
;
rewrite($filename,$rest);
return ('BOM found and automatically removed');
}else{
return (' BOM found');
}
}else{
return ('BOM not found');
}
}
function rewrite($filename,$data){
$filenum=fopen($filename,'w');
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>
Code three:
Copy code
$auto = 1;
checkdir ( $basedir );
function checkdir($basedir) {
if ($dh = opendir ( $basedir )) {
while ( ($file = readdir ( $dh )) ! == false ) {
If it is a file
echo "filename: $basedir/$file " . checkBOM ( "$basedir/$file" ) . "
";
} else {
$dirname = $basedir . "/" . $file; // If it is a directory
( $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) { // BOM
// code separation for
$rest = substr ($contents, 3);
rewrite ( $filename, $rest );
return ("BOM found, automatically removed.");
use use use using using using using using using out out out out out out out out out out ‘’’ out. =red>BOM found.");
}
} else
; data) {
$filenum = fopen ($filename, "w");
flock ($filenum, LOCK_EX);
fwrite ($filenum, $data);
fclose ($filenum) ;
}
?>
2. Python Copy code The code is as follows:
import os
def delBOM():
file_count = 0
bom_files = []
for dirpath, dirnames, filenames in os.walk('.'):
if(len(filenames)):
for filename in filenames:
file_count += 1
file = open(dirpath + "/" + filename, 'r+')
file_contents = file.read()
if(len(file_contents) > 3):
if(ord(file_contents[0]) == 239 and ord(file_contents[1]) == 187 and ord(file_contents[2]) == 191):
bom_files.append(dirpath + "/" + filename)
file.seek(0)
file.write(file_contents[3:])
print bom_files[-1], "BOM found. Deleted."
file.close()
print file_count, "file(s) found.", len(bom_files), "file(s) have a bom. Deleted."
if __name__ == "__main__":
delBOM()