本文将带您了解关于浅谈php中fopen不能创建中文文件名文件的问题的新内容,同时我们还将为您解释phpfopen失败的相关知识,另外,我们还将为您提供关于firefox中中文文件名乱码或不能解码的问
本文将带您了解关于浅谈php中fopen不能创建中文文件名文件的问题的新内容,同时我们还将为您解释php fopen失败的相关知识,另外,我们还将为您提供关于firefox中中文文件名乱码或不能解码的问题、fopen创建中文TXT文件时出错、java创建中文文件名文件、OpenProject 上传「中文文件名」不能正确显示的问题的实用信息。
本文目录一览:- 浅谈php中fopen不能创建中文文件名文件的问题(php fopen失败)
- firefox中中文文件名乱码或不能解码的问题
- fopen创建中文TXT文件时出错
- java创建中文文件名文件
- OpenProject 上传「中文文件名」不能正确显示的问题
浅谈php中fopen不能创建中文文件名文件的问题(php fopen失败)
之前网页的chartset用的是utf-8,文件也用utf-8,然后用fopen()创建一个中文文件名的文件时问题就出来了,文件名都是乱 码!
查看了很多文档试了不少方法都解决不了,本来想着用别的方法绕过这个问题,忽然脑子里闪过Windows默认的文字编码是ansi,然后再 baidu了一下,证实了这点,所以我的网页也应该是ansi编码才能使创建的文件名不会是乱码。
接着就着手验证,把网页都用ansi保存,去掉chartset语句,果然ok了,但是网页的内容就成乱码了,后来想起,这个网页还include 了别的网页,把include的网页也改成ansi保存,哈哈万事ok
编程这个工作真的很靠积累,如果我以前没看过Windows默认编码是ansi,那这个问题就不知何年何月才能解决了
ps:< meta content ="text/html; charset=utf-8" http -equiv ="Content-type" > 这个Meta标记一定要放在
后来又想到了一个更好的解决方法,网页还是用utf-8编码和保存,只是fopen()里的文件名参 数单独给它编下码就行,PHP有iconv() 这个改换编码的程序,把utf-8转成 gb2312就可以避免中文文件名为乱码了
test.htm
test.PHP
$rows = array(array("替换标题1","替换内容1"),array("替换标题2","替换内容2"));
$filename = "tmp.htm";
foreach($rows as $id => $val){
$title = $val[0];
$content = $val[1];
$pagename = "测试".$id.".html";
//对文件名的编码,避免中文文件名乱码
$pagename = iconv("UTF-8","GBK",$pagename);
//读取模板
$tmpfile = fopen($filename,"r");
$string = fread($tmpfile,filesize($filename));
$string = str_replace("{title}",$title,$string);
$string = str_replace("{content}",$content,$string);
fclose($tmpfile);
//写新<a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>
$newpage = fopen($pagename,"w");
fwrite($newpage,$string);
fclose($newpage);
}
echo "创建成功!";
?>
以上这篇浅谈PHP中fopen不能创建中文文件名文件的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持小编。
firefox中中文文件名乱码或不能解码的问题
firefox解决下载有中文名的文件文件名解码问题
添加响应头并用UTF-8编码文件名
response.setHeader("content-disposition","attachment; filename*=UTF-8''''" + URLEncoder.encode(fileName,"UTF-8"));
fopen创建中文TXT文件时出错
$pagename="异路迷途.txt";
fp=fopen($pagename,"r");
这样后提示
Warning: fopen(异路迷途.txt) [function.fopen]: failed to open stream: Invalid argument in D:\code\book\phpcms\modules\book\index.php on line 44
Warning: fwrite() expects parameter 1 to be resource, boolean given in D:\code\book\phpcms\modules\book\index.php on line 50
如果是数字名称就可以
回复讨论(解决方案)
iconv("utf-8","gb2312",$filename);
字符集问题吧
java创建中文文件名文件
java在linux环境创建中文文件名文件,文件UTF-8编码,代码如下:
private static void createCnFile() { try { String fileName = "中文名文件.txt"; String newName = new String(fileName.getBytes(), "UTF-8"); File f1 = new File(newName); if (!f1.exists()) { f1.createNewFile(); } }catch (Exception e) { } }
windows中创建中文文件名文件,文件GBK编码,代码如下:
private static void createCnFile() { try { String fileName = "中文名文件11.txt"; File f1 = new File(fileName); if (!f1.exists()) { f1.createNewFile(); } }catch (Exception e) { } }
OpenProject 上传「中文文件名」不能正确显示的问题
从 2007 年开始用 Redmine,最近半年一直想换一个平台,团队用了三个多月的 Worktile,不太习惯。最近一个月考察了 Trello、Tower、Teambition、OpenProject,最后决定试用 OpenProject 一周。上周六用了七个多小时完成手工配置,简单试用下来,Timeline、Report、Backlogs 等功能都是极好的。
美中不足的是:上传附件如果是「中文文件名」不能正常显示。不会 Ruby 和 Rails,Google 之后通过修改源码的方式解决了这个问题。
修改方案
OpenProject 使用了 CarrierWave 实现上传文件功能,但是 CarrierWave 默认仅支持英语字母、数字、空格
。如果要支持汉语需要重写 sanitize_regexp
方法。
Filenames and unicode chars
Another security issue you should care for is the file names (see Ruby On Rails Security Guide). By default, CarrierWave provides only
English letters, arabic numerals and some symbols as white-listed characters in the file name
. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you have to overridesanitize_regexp
method. It should return regular expression which would match all non-allowed symbols.Also make sure that allowing non-latin characters won''t cause a compatibility issue with a third-party plugins or client-side software.
知道了解决方法,需要结合 OpenProject 代码才能确认具体修改哪个文件。
A 方案(确认可行)
在 /config/initializers/carrierwave.rb
文件最后添加一行代码
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
B 方案(未确认)
修改 /app/uploaders/fog_file_uploader.rb
和 /app/uploaders/local_file_uploader.rb
文件,添加以下代码:
def sanitize_regexp
/[^0-9\.\-\+_]/
end
参考资料
CarrierWave Github
CarrierWaveを使ってファイルアップロード
:sanitize_regexp doesn''t works
file.original_filename上传文件,文件名中文乱码怎么解决的啊
今天的关于浅谈php中fopen不能创建中文文件名文件的问题和php fopen失败的分享已经结束,谢谢您的关注,如果想了解更多关于firefox中中文文件名乱码或不能解码的问题、fopen创建中文TXT文件时出错、java创建中文文件名文件、OpenProject 上传「中文文件名」不能正确显示的问题的相关知识,请在本站进行查询。
本文标签: