Because the file header information outputs the BOM header information and sometimes affects the execution result of the program, the BOM information of these files should be removed. Because the file header information outputs the BOM header information and sometimes affects the execution result of the program, the BOM information of these files should be removed.
Script ec (2); script
The following code uses PHP to remove the BOM information of all files in the current directory and sub-directories, create a file, put it under the root directory, and then access the file through a browser.
Example
Save the above Code as a php file and put it in the directory where the bom needs to be removed, and then run the php file, check the bom of the Directory and all the files in the subdirectories of the Directory and remove the bom.
| The Code is as follows: |
|
|
If (isset ($ _ GET ['dir']) {// sets the 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 ")."
";
} 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 ("BOM found, automatically removed. _ http://www.111cn.net ");
} 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 );
}
?> |
Example 2
| The Code is as follows: |
|
|
Header ('content-Type: text/html; charset = UTF-8 ');
$ Auto = 1;/* set to 1: Check BOM and remove it. set to 0: Check BOM only */
$ Basedir = '.';
$ Loop = true; // www.111cn.net echo 'the current directory is:'. $ basedir. 'The current setting is :';
Echo '(1)', $ loop? 'Check the current directory and Its subdirectories ': 'check the current directory only ';
Echo '(2)', $ auto? 'Check the file BOM while removing the BOM of the detected BOM File
':' Only checks the file BOM and does not delete the BOM.
';
Checkdir ($ basedir, $ loop );
Function checkdir ($ basedir = '', $ loop = true ){
$ Basedir = empty ($ basedir )? '.': $ Basedir;
If ($ dh = opendir ($ basedir )){
While ($ file = readdir ($ dh ))! = False ){
If ($ file! = '.' & $ File! = '..'){
If (! Is_dir ($ basedir. '/'. $ file )){
Echo 'file: '. $ basedir.'/'. $ file. checkBOM ($ basedir.'/'. $ file ).'
';
} Else {
If (! $ Loop) continue;
$ 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 ('found BOM and removed automatically ');
} Else {
Return ('Find BOM ');
}
} Else {
Return ('bom not found ');
}
}
Function rewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, 'w ');
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
} |