本文介绍了如何在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

我应该怎么做?

推荐答案

只是许多字符(使用组合字符)。如果您不对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 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.

    将您的文件/目录名称限制为。实际上,您将通过,然后在文件系统功能中使用它们,并传递条目通过获得原始文件名为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.

    注意事项!


    • 如果传递到文件系统功能的任何字节匹配,您没有运气。

    • 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字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-18 05:12
    查看更多