問題描述
我無法使用 mkdir
創建帶有 UTF-8 字符的文件夾:
I can't use mkdir
to create folders with UTF-8 characters:
<?php
$dir_name = "Depósito";
mkdir($dir_name);
?>
當我在 Windows 資源管理器中瀏覽此文件夾時,文件夾名稱如下所示:
when I browse this folder in Windows Explorer, the folder name looks like this:
Dep?3sito
我該怎么辦?
我正在使用 php5
推薦答案
Just urlencode
需要作為文件名的字符串. 從 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):
- 經過 url 編碼后,文件名必須少于 255 個字符(可能是字節).
- UTF-8 對許多字符具有多種表示(使用組合字符).如果您不規范化 UTF-8,則可能無法使用
glob
進行搜索或重新打開單個文件. - 您不能依賴
scandir
或類似函數進行 alpha 排序.您必須urldecode
文件名,然后使用識別 UTF-8(和排序規則)的排序算法.
- 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 musturldecode
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:
在您的文件名中自由使用 UTF-8,但要了解非 ASCII 字符在 PHP 之外看起來不正確.非 ASCII UTF-8 字符將存儲為多個 單個 ISO-8859-1 字符.例如.
ó
在 Windows 資源管理器中將顯示為?3
.
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?3
in Windows Explorer.
將您的文件/目錄名稱限制為字符可在 ISO-8859-1 中表示.在實踐中,您將在使用之前通過 utf8_decode
傳遞 UTF-8 字符串在文件系統函數中,并傳遞條目 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.
大量警告!
- 如果傳遞給文件系統函數的任何字節匹配無效的WindowsISO-8859-1 中的文件系統字符,你運氣不好.
- 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 ofutf8_decode
.
這個噩夢就是為什么你應該音譯來創建文件名.
This nightmare is why you should probably just transliterate to create filenames.
這篇關于如何使用 UTF-8 字符串在 PHP 中使用文件系統函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!