如何在PHP中使用文件系统功能,使用UTF-8字符串? [英] How do I use filesystem functions in PHP, using UTF-8 strings?

查看:141
本文介绍了如何在PHP中使用文件系统功能,使用UTF-8字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用mkdir创建具有UTF-8字符的文件夹。

I can't use mkdir to create folders with UTF-8 characters.

<?php

$dir_name = "Depósito";
mkdir($dir_name );

?>

但是,当我在Windows资源管理器中浏览此文件夹时,文件夹名称如下所示:

But, when I browse this folder in Windows Explorer, the folder name looks like this:

Depósito

我应该怎么做?

推荐答案

只是 >从 urlencode 返回的字符在文件名(NTFS / HFS / UNIX)中有效,那么您只需 urldecode 文件名返回到UTF-8(或其中的任何编码)。

Just urlencode the string desired as a filename. All characters returned from urlencode are valid in filenames (NTFS/HFS/UNIX), then you can just urldecode the filenames back to UTF-8 (or whatever encoding they were in).

注意事项(均适用于以下解决方案):

Caveats (all apply to the solutions below as well):

  • After url-encoding, the filename must be less that 255 characters (probably bytes).
  • UTF-8 has multiple representations for many characters (using combining characters). If you don't normalize your UTF-8, you may have trouble searching with glob or reopening an individual file.
  • You can't rely on scandir or similar functions for alpha-sorting. You must urldecode the filenames then use a sorting algorithm aware of UTF-8 (and collations).

以下是不太有吸引力的解决方案,更复杂,并有更多的注意事项。

The following are less attractive solutions, more complicated and with more caveats.

在Windows上,PHP文件系统包装器期望并返回文件/目录名称的ISO-8859-1字符串。这给您两个选择:

On Windows, the PHP filesystem wrapper expects and returns ISO-8859-1 strings for file/directory names. This gives you two choices:


  1. 在您的文件名中自由使用UTF-8,但了解非ASCII字符将>在PHP外面显示不正确。非ASCII UTF-8字符将作为多个单个 ISO-8859-1字符存储。例如。 ó将在Windows资源管理器中显示为ó

  1. Use UTF-8 freely in your filenames, but understand that non-ASCII characters will appear incorrect outside PHP. A non-ASCII UTF-8 char will be stored as multiple single ISO-8859-1 characters. E.g. ó will be appear as ó in Windows Explorer.

将您的文件/目录名称限制为可以代表的字符ISO-8859-1 。实际上,您将通过 utf8_decode ,然后在文件系统功能中使用它们,并传递条目 scandir 通过 utf8_encode 获得原始文件名为UTF-8。

Limit your file/directory names to characters representable in ISO-8859-1. In practice, you'll pass your UTF-8 strings through utf8_decode before using them in filesystem functions, and pass the entries scandir gives you through utf8_encode to get the original filenames in UTF-8.

注意事项!


  • 如果传递到文件系统功能的任何字节匹配 ISO-8859-1中无效的Windows文件系统字符,您没有运气。

  • Windows 可能会使用其他编码比非英语语言环境中的ISO-8859-1。我猜这通常是ISO-8859-#之一,但这意味着你需要使用 mb_convert_encoding 而不是 utf8_decode

  • If any byte passed to a filesystem function matches an invalid Windows filesystem character in ISO-8859-1, you're out of luck.
  • Windows may use an encoding other than ISO-8859-1 in non-English locales. I'd guess it will usually be one of ISO-8859-#, but this means you'll need to use mb_convert_encoding instead of utf8_decode.

这个噩梦是为什么你应该只是音译创建文件名。

This nightmare is why you should probably just transliterate to create filenames.

这篇关于如何在PHP中使用文件系统功能,使用UTF-8字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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