"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > How Can I Handle UTF-8 Filenames in PHP's Filesystem Functions?

How Can I Handle UTF-8 Filenames in PHP's Filesystem Functions?

2025-03-26에 게시되었습니다
검색:289

How Can I Handle UTF-8 Filenames in PHP's Filesystem Functions?

Handling UTF-8 Filenames in PHP's Filesystem Functions

When creating folders containing UTF-8 characters using PHP's mkdir function, you may encounter display issues in Windows Explorer due to encoding incompatibilities.

Solution: URL Encode Filenames

To resolve this issue, use the urlencode function to convert the desired folder name to a URL-safe format before passing it to mkdir:

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

This ensures that all characters in the folder name are encoded into valid filenames for all operating systems. To retrieve the original UTF-8 filename, use urldecode.

Alternative Solutions (with Caveats)

While URL encoding is the recommended solution, there are less attractive alternatives:

Convert to ISO-8859-1 (Windows Only)

On Windows, you can work with UTF-8 filenames but be aware that non-ASCII characters will appear incorrectly outside of PHP. To address this, use utf8_decode to convert filenames to ISO-8859-1 before using them in filesystem functions.

Restrict to ISO-8859-1 Characters

Alternatively, limit filenames to characters representable in ISO-8859-1. Use utf8_decode and utf8_encode to convert filenames between UTF-8 and ISO-8859-1.

However, these alternatives come with caveats:

  • Filesystem functions cannot handle bytes that match invalid Windows filesystem characters in ISO-8859-1.
  • Windows may use non-ISO-8859-1 encodings in non-English locales, requiring more complex conversion.
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3