PHP batch detection and removal of file BOM header information code

Source: Internet
Author: User
Tags flock ord php file

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: Copy code

<? Php
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"). "<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.111cn.net "> http://www.111cn.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 );
}
?>

Example 2

The code is as follows: Copy code

<? Php
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 and remove the BOM detected in the BOM file <br/> ': 'Only check the file BOM and do not perform the BOM removal operation. <br/> ';

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).' <br> ';
} 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 ('<font color = red> Find the BOM and remove it automatically </font> ');
} Else {
Return ('<font color = red> find BOM </font> ');
  }
} Else {
Return ('Bom not found ');
 }
}
Function rewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, 'w ');
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.