PHP batch removal of File Header Bom information

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about the websites developed in the linux environment. To port them to the windows environment, deploy them, the verification code image is not displayed in windows. It is normal in linux. The reason for this is that the bom information is contained in the header of the file to be loaded, so that it cannot be displayed, so I thought of writing a bulk replacement File Header bom information. Share with you ~

What is a file header Bom?Do not know how to fill your shoes: view the article
To put it bluntly, there will be a hidden string of characters in front of the file when saving the file. However, it is too troublesome to manually edit so many website files, use the following program to batch remove the file header Bom:

File Name: bom. class. php /**
* Batch remove the file header bom.
* Author: Simon
* E-mail: vsiryxm@qq.com
* Date:
*/

Class Bom {
Static public $ total = 0; // statistics on the number of files
Static public $ count = 0; // Number of replacements

Protected $ config = array (
'Auto' => 1, // whether to automatically replace 1 with automatically replace
'Dir' => '.', // the directory to be traversed defaults to the current
'R' => 1, // 1 is recursive
);

Function _ construct (){
If (isset ($ _ REQUEST ['auto']) {
$ This-> config ['auto'] =_ _ REQUEST ['auto'];
}
If (! Empty ($ _ REQUEST ['dir']) {
$ This-> config ['dir'] = $ _ REQUEST ['dir'];
}
If (isset ($ _ REQUEST ['R']) {
$ This-> config ['R'] = $ _ REQUEST ['R'];
}
}

// Set
Public function set ($ key, $ value = ''){
If (isset ($ this-> config [$ key]) {
$ This-> config [$ key] = $ value;
}
}

// Traverse the files in the directory and replace the bom
Public function remove ($ curr_dir = ''){
$ Dir =! Empty ($ curr_dir )? $ Curr_dir: $ this-> config ['dir'];
If ($ files = opendir ($ dir )){
Ob_end_flush (); // directly outputs the buffer content
While ($ file = readdir ($ files ))! = False ){
If ($ file! = '.' & $ File! = '..'){
// Directory Recursion
If (is_dir ($ dir. DIRECTORY_SEPARATOR. $ file) & $ this-> config ['R'] = 1 ){
$ This-> remove ($ dir. DIRECTORY_SEPARATOR. $ file );
}
Elseif (! Is_dir ($ dir. DIRECTORY_SEPARATOR. $ file )){
Self: $ total ++;
If ($ content = $ this-> checkBOM ($ dir. DIRECTORY_SEPARATOR. $ file )){
If ($ this-> config ['auto'] = 1 ){
$ Content = substr ($ content, 3 );
$ This-> rewrite ($ dir. DIRECTORY_SEPARATOR. $ file, $ content );
Echo ''. $ dir. DIRECTORY_SEPARATOR. $ file. 'has been replaced!
'. PHP_EOL;
Self: $ count ++;
}
Else {
Echo ''. $ dir. DIRECTORY_SEPARATOR. $ file. 'has a Bom!
'. PHP_EOL;
}
}
Else {
Echo $ dir. DIRECTORY_SEPARATOR. $ file. 'No Bom!
'. PHP_EOL;
}
}
}
Flush ();
// Sleep (1 );
}
Closedir ($ files );
}
Else {
Echo 'check the path does not exist! ';
}
}

// Check Bom
Public function checkBOM ($ filename ){
$ Content = file_get_contents ($ filename );
If (! Empty ($ content )){
$ Charset [1] = substr ($ content, 0, 1 );
$ Charset [2] = substr ($ content, 1, 1 );
$ Charset [3] = substr ($ content, 2, 1 );
If (ord ($ charset [1]) = 239 & ord ($ charset [2]) = 187 & ord ($ charset [3]) = 191) {
Return $ content;
}
}
Return false;
}

// Rewrite the file
Public function rewrite ($ filename, $ data ){
$ File = fopen ($ filename, "w ");
Flock ($ file, LOCK_EX );
Fwrite ($ file, $ data );
Fclose ($ file );
}

}

//////////////////////////////////////// ////////
// Call
$ Bom = new Bom ();

Echo <









Start checking Bom...


EOF;

$ Bom-> remove ();

Echo 'script document. getElementById (\ 'result \ '). innerHTML = \' detection complete! A total of '. Bom: $ total.' files are replaced with '. Bom: $ count.' files \ '; script';
Echo <




EOF;
$ Bom = null;
?>
From the above class, we can see that during the call, we use the default parameters (the current directory, can be recursive, automatically removed) to run the program, you can also set parameters to run:

Method 2: // Call
$ Bom = new Bom ();
$ Bom-> set ('auto', 0); // check only
$ Bom-> set ('dir', './test'); // test directory under the current directory
$ Bom-> set ('R', 0); // do not Recursively search for subdirectories
Method 3:
Http: // your domain name/bom. php? Auto = 1 & dir =./test/& r = 1

Running effect:






Attachment: Batch removal of the file header Bom information package >>>
Upload the file to any directory of the website and access and run it in the browser. Back up the site file before running, to ensure that the file to be replaced can be written.
Click to download:

Bom.class.zip (1.78 KB download: 30 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.