file_put_contents 保存文件时乱码

简介: tomcat让人抓狂,后台java写的一个应用程序生成的静态html居然是ANSI编码格式的文件,前台首页点击查看页面时直接乱码了…   使用新的tomcat、重新配置,然后放在webapp下重新弄,不使用Catalina/localhost的方式,依旧不行。

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: ?>
目录
相关文章
|
1月前
|
编译器 API C语言
C/C++ 获取文件名的方法:分享一些实用的获取文件名的方法和技巧(__FILE__,__builtin_FILE(),__BASE_FILE__等)
C/C++ 获取文件名的方法:分享一些实用的获取文件名的方法和技巧(__FILE__,__builtin_FILE(),__BASE_FILE__等)
46 0
|
5月前
|
NoSQL Linux
gdb调试产生code文件以及遇到的“file format not recognized”问题解决
gdb调试产生code文件以及遇到的“file format not recognized”问题解决
291 0
|
5月前
指定的 filePath 文件不存在
指定的 filePath 文件不存在
23 2
|
9月前
file_get_contents和strstr防止文件关键内容被删除
file_get_contents和strstr防止文件关键内容被删除
19 0
|
存储 Python
python2:open()文件名为中文,报错IOError: [Errno 22] invalid mode ('w') or filename
python2:open()文件名为中文,报错IOError: [Errno 22] invalid mode ('w') or filename
409 0
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
|
移动开发 PHP
file_put_contents追加 一个很简单的php记录日志的函数
$str="我是日志内容!"; $str=$str.date("Y-m-d H:i:s",time())."\r\n"; file_put_contents('log.txt',$str,FILE_APPEND); file_put_contents() 的行为实际上等于依次调用 fopen(),fwrite() 以及 fclose() 功能一样。 FILE_APPEND:在文件末尾以追加的方式写入数据 ———————————————— 版权声明:本文为CSDN博主「娃娃菜001」的原创文章,遵循CC
183 0
file_put_contents追加 一个很简单的php记录日志的函数
|
JavaScript
解决ecshop出现Warning: file_put_contents
解决ecshop出现Warning: file_put_contents
232 0