PHP检测文件系统编码 [英] PHP detecting filesystem encoding

查看:183
本文介绍了PHP检测文件系统编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PHP将不带文件名的文件保存在filesytem文件中。



我想使这个工作跨平台。我怎么知道我可以用什么编码来编写文件?我知道许多现代的文件系统是基于UTF-8的(这是正确的?),但我怀疑Windows XP是(例如)。

那么,是否有一个强大的检测如果你不需要在文件系统级别进行大量的操作(比如说,搜索,排序...),对于 this so question URLEncode()文件名。

 Hörensägen.txt

变成

  H%c3%b6rens%c3%a4gen.txt 
code>

在任何文件系统中都应该是安全的,并且可以映射任何UTF-8字符。 b

我觉得这比尝试本机地处理主机操作系统的能力要好得多,这个操作系统保证了复杂且容易出错的问题(除了操作系统的差异之外,我敢肯定,文件系统格式 - FAT16,FAT32,NTFS,extFS版本1/2/3 ....自带一套规则要注意。)

I need to save files with non-latin filenames on a filesytem, using PHP.

I want to make this work cross-platform. How do I know what encoding I can use to write the file? I understand many modern filesystems are UTF-8 based (is this correct?), but I doubt Windows XP is (for instance).

So, is there a robust detection mechanism?

解决方案

Not an answer to your question, but if you don't need to do extensive operations on filesystem level (like searching, sorting...), there is a nice cross-platform workaround for the issue outlined in this SO question: URLEncode()ing file names.

Hörensägen.txt 

gets turned into

H%c3%b6rens%c3%a4gen.txt

which should be safe to use in any filesystem and is able to map any UTF-8 character.

I find this much preferable to trying to "natively" deal with the host OS's capabilities, which is guaranteed to be complicated and error-prone (in addition to operating system differences, I'm sure the various filesystem formats - FAT16, FAT32, NTFS, extFS versions 1/2/3.... bring their own set of rules to be aware of.)

这篇关于PHP检测文件系统编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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