将文件名保存为unicode字符 [英] Save filename with unicode chars

查看:274
本文介绍了将文件名保存为unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了所有的互联网和SO,仍然没有运气如下:

I have searched all over the Internet and SO, still no luck in the following:

我想知道,如何正确保存文件使用<$当文件名有一些unicode字符时,c $ c> file_put_contents (Windows 7作为操作系统)

I would like to know, how to properly save a file using file_put_contents when filename has some unicode characters. (Windows 7 as OS)

$string = "jérôme.jpg" ; //UTF-8 string
file_put_contents("images/" . $string, "stuff");

重新发送文件:

jГ©rГґme.jpg

尝试所有可能的组合, iconv mb_convert_encoding 与所有可能的编码,将源文件转换为不同的编码。
所有适当的标题设置,浏览器正确识别 UTF-8

Tried all possible combinations of such functions as iconv and mb_convert_encoding with all possible encodings, converting source file into different encodings as well. All proper headers are set, browser recognises UTF-8 properly.

但是,我可以成功复制粘贴并在浏览器的GUI中创建一个具有这样一个名称的文件,但是如何通过PHP进行创建?

However, I can successfully copy-paste and create a file with such a name in explorer's GUI, but how to make it via PHP?

最后一个硬核解决方案是 urlencode 字符串和保存文件。

The last hardcore solution was to urlencode the string and save file.

推荐答案

但是我刚刚找到了一个解决方案来解决我这个伤害问题。
忘记关于 iconv 多字节解决方案; 在Windows上的问题! (在链接中,你会发现这一切都是美丽的。)

This might be late but i just found a solution to close this hurting issue for me as well. Forget about iconv and multibyte solutions; the problem is on Windows! (in the link you'll find all it's beauty about this.)

经过多次尝试和解决方法,我遇到了 URLify ,并决定最好的方式来处理unicode-in-filenames是在写入文件之前将音译为。

After numerous attempts and ways to solve this, i met with URLify and decided that best way to cope with unicode-in-filenames is to transliterate them before writing to file.

保存文件名之前的音译示例:

Example of transliterating a filename before saving it:

$filename = "Αρχείο.php";   // greek name for 'file'
echo URLify::filter($filename,128,"",TRUE);
// output:   arxeio.php

这篇关于将文件名保存为unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆