雷达智富

首页 > 内容 > 网页技术 > 正文

网页技术

一键删除字符造成的顶部空白行

2021-11-19 446

一键删除&#65279字符造成的顶部空白行:

把代码复制并新建一个php文件保存上传到网站根目录,接着访问这个php文件,就可以执行检查,文件较多的请耐心等待。

温馨提示:执行完检查和删除后请删除这个新建的php文件,要不然会暴露网站目录和文件,执行前请先备份好程序,以免出错。

<?php   
if (isset($_GET['dir'])){ //config the basedir
   $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.</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);
}


赞一波!1

文章评论

全部评论