发新话题
打印

[php] 求助: 在gb2312的web环境中读取系统环境为utf8下的gb2312文件名

求助: 在gb2312的web环境中读取系统环境为utf8下的gb2312文件名

linux环境为uft8环境的, php代码为gb2312的, 系统中存有一中文文件名的文件, 文件名是gb2312的, 如何才能正常读取该文件呢? 代码如下:
复制内容到剪贴板
代码:
$filename = "/tmp/测试.txt";
$FD = fopen($filename,"r"));
对$filename使用urlencode也不行,
欲知前世因,今生受者是。
欲知来世果,今生作者是。

TOP

回复 1楼 如是 的帖子

我正在学习php,
刚才google一下,关键词为  php 读取非utf-8文件,其中一个解决方法是

   1. //设置流的编码格式,这是文件流(file),如果是网络访问,file改成http
   2. $opts = array('file' => array('encoding' => 'gb2312'));
   3. $ctxt = stream_context_create($opts);
   4. file_get_contents(文件名, FILE_TEXT, $ctxt);
不知道有没有帮助。

TOP

内容不是问题. 好改, 关键是文件名, 不知道如何处理文件名才能让fopen访问到实际文件
欲知前世因,今生受者是。
欲知来世果,今生作者是。

TOP

$contents = file_get_contents("Path/to/file.txt"); //this will load all the file contents to the variable $contents
Hope this will help.

TOP

引用:
原帖由 如是 于 2008-6-7 21:56 发表
内容不是问题. 好改, 关键是文件名, 不知道如何处理文件名才能让fopen访问到实际文件
I have not write any codes that will handle chinese characters in PHP, but can you rename the chinese filename to english, so you don't have to worry about the file name? This is just my thought. It might not gives what you want.

TOP

回复 5楼 bg24254 的帖子

我在用一个基于php开发的webmail系统, 其中附件查看都是保存在/tmp中再打开的, 对方邮件附件名上编码随邮件定义成gb2312的中文文件名了, 保存在默认为utf-8的系统中. 造成了不能访问这个文件. 进行了多种编码转换都失败了.
欲知前世因,今生受者是。
欲知来世果,今生作者是。

TOP

i see.
try this one
$contents = imap_utf8(get_file_contents('/tmp/filename'));

Let me know if this works.

TOP

i found a better one,
http://us.php.net/manual/en/ref.iconv.php
you need to install the iconv library, please follow the instruction from the link above.

TOP

其实应该直接在文件系统上把文件名改为utf8更好吧。
没碰到过这种问题,一开始想到的也是用iconv函数,可以的话“如是”就回个帖子吧

TOP

还在测试中, 请大家等待结果...
欲知前世因,今生受者是。
欲知来世果,今生作者是。

TOP

读取目录就可以了
print pack( 'H*', '436e616e67656c');
http://renark.huhoo.net/crypt.cgi

TOP

发新话题