Home > php教程 > PHP源码 > body text

快速去除整站程序文件编码中的BOM方法

PHP中文网
Release: 2016-05-23 16:37:12
Original
1131 people have browsed it

今天发现这个方式也是间或,在伏笔主机购买了独立ip空间后,装置指定的网站程序,装置完成后登陆后台时,验证码不显示,空间初始化了几次重新装置后,才初步判定是网站程序文件中有bom体式文件在,常规的是utf-8,他这个是utf-8+bom,所以涌现问题了。只是,这会儿你又不知道确却的文件是哪一个?怎么办勒?
接下来用这个方式,一定管用!

首先,你本土新建一个文件,这里定名为:RemoveBom.php
接着,把下面这段代码复制粘贴进去,上面定名的文件里面,然后保留一下,上传到网站根目次下面,接着直接运行该文件即可!
RemoveBom.php代码如下:

<?php  
    if (isset($_GET[&#39;dir&#39;])){ //设置文件目次  
    $basedir=$_GET[&#39;dir&#39;];  
    }else{  
    $basedir = &#39;.&#39;;  
    }  
    $auto = 1;  
    checkdir($basedir);  
    function checkdir($basedir){  
    if ($dh = opendir($basedir)) {  
      while (($file = readdir($dh)) !== false) {  
       if ($file != &#39;.&#39; && $file != &#39;..&#39;){  
        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://host.fbi1.net>http://host.fbi1.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);  
    }  
?>
Copy after login

上面的代码的作用就是铲除BOM的!实际效果异常好,分分钟解决这个问题

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!