<? Php // This file is used to quickly test whether the UTF-8 encoded file is added with BOM and can be automatically removed. $ Basedir = "."; // modify the directory to be checked for this behavior. The vertex indicates the current directory. $ Auto = 1; // whether to automatically remove the detected BOM. 1 is yes, 0 is no. // Do not change the following 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 ); } // End ?>
|