file_put_contents 保存文件时乱码

tomcat让人抓狂,后台java写的一个应用程序生成的静态html居然是ANSI编码格式的文件,前台首页点击查看页面时直接乱码了…

 

使用新的tomcat、重新配置,然后放在webapp下重新弄,不使用Catalina/localhost的方式,依旧不行。。得,同事机器上可以跑,而且正常,就我机器跑不起来。因为要频繁更改模板文件,所以不太好用nginx反向代理到同事的机器上,最好能在本机跑..

 

所以决定用php把那些该死的乱码文件全部全部转一下编码再保存…

在使用file_put_contents的时候,遇到比较郁闷的问题,文件格式是对了,但里面却乱码了,后面想想,觉得应该是先删除文件再进行处理,试了一下,文件格式正确、内容正确。

 

在使用iconv函数时,先开始用gb2312->utf-8发现不行,部分字符串无法读入进去,然后抱着试试的心态,使用gbk->utf-8居然行了~ ~

---注:在转码前需要判定该文件编码格式是否为utf-8,如果为utf-8转码反而出错

 

花几分钟写的,有点乱,达到预期目标就成。

 
   1: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   2:  
   3: <?php
   4:     
   5:     function reSaveFile($path) {
   6:         
   7:         if (is_dir($path)) {
   8:             
   9:             if ($handle = opendir($path)) {
  10:                 while (($file = readdir($handle)) !== FALSE) {
  11:                     
  12:                     if ($file != "." && $file != "..") {
  13:                         $fileName = $path."/".$file;
  14:                         
  15:                         if (is_file($fileName) && preg_match("/.html$/", $file)) {
  16:                             echo "<div>正在处理文件:".$fileName."</div>";
  17:                             
  18:                             saveHandler($fileName);
  19:                         } else if (is_dir($fileName)) {
  20:                             echo "<div style='color:#406c99;'>文件夹".$fileName."</div>";
  21:                             
  22:                             reSaveFile($fileName);
  23:                         } else {
  24:                             echo "<div style='color:#f00;'>跳过的文件:".iconv("gbk", "utf-8", $fileName)."</div>";                            
  25:                         }
  26:                         
  27:                     }
  28:                     
  29:                 }
  30:                 
  31:                 closedir($path);
  32:             }
  33:             
  34:         } else {
  35:             echo "1";
  36:         }
  37:         
  38:     }
  39:     
  40:     function isUTF8($str) {
  41:         if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32")) {
  42:             return true;
  43:         } else {
  44:             return false;
  45:         }
  46:     }
  47:     
  48:     function saveHandler($fileName) {
  49:         $mode = "r";
  50:         
  51:         $file_content = file_get_contents($fileName);
  52: //        $file_content = strip_tags($file_content);
  53:         
  54:         if (isUTF8($file_content)) {
  55:             echo "<div>跳过utf-8文件:".$fileName."</div>";
  56:             return ;
  57:         }
  58:         
  59:         $file_content = iconv("gbk", "utf-8", $file_content);
  60:         
  61:         if (file_exists($fileName)) {
  62:             unlink($fileName);
  63:         }
  64:         
  65:         file_put_contents($fileName, $file_content);
  66:         
  67:         echo "<a href='".$fileName."'>".$fileName."</a>保存成功";
  68:         
  69:         echo "<textarea>".$file_content."</textarea>";
  70:     }
  71:     
  72:     reSaveFile("C:/AEApps/application/GTJ_GW3");
  73: ?>
posted @ 2011-04-26 11:16  meteoric_cry  阅读(11477)  评论(0编辑  收藏  举报