简体   繁体   English

PHP 文件名(或其完整路径中的目录)可以包含 UTF-8 字符吗?

[英]Can a PHP file name (or a dir in its full path) have UTF-8 characters?

I would like to access a PHP file whose name has UTF-8 characters in it.我想访问一个名称包含 UTF-8 字符的 PHP 文件。

The file does not have a BOM in it.该文件中没有 BOM。 It just contains an echo statement that displays a few unicode characters.它只包含一个显示几个 unicode 字符的 echo 语句。

Accessing the PHP page from the browser (FireFox 3.0.8, IE7) results in HTTP error 500.从浏览器(FireFox 3.0.8、IE7)访问 PHP 页面会导致 HTTP 错误 500。

There are two entries in the Apache log (file is /க.php; the letter க is a composite one and corresponds to the characters \xe0\xae\x95 in the log below): Apache 日志中有两个条目(文件为 /க.php;字母 க 是复合条目,对应于下面日志中的字符 \xe0\xae\x95):

[Sat Apr 04 09:30:25 2009] [error] [client 127.0.0.1] PHP Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 [2009 年 4 月 4 日星期六 09:30:25] [错误] [客户端 127.0.0.1] PHP 警告:未知:无法打开 stream:第 0 行的未知中没有此类文件或目录

[Sat Apr 04 09:30:25 2009] [error] [client 127.0.0.1] PHP Fatal error: Unknown: Failed opening required 'D:/va/ROOT/\xe0\xae\x95.php' (include_path='.;C:\php5\pear') in Unknown on line 0 [2009 年 4 月 4 日星期六 09:30:25] [错误] [客户端 127.0.0.1] PHP 致命错误:未知:打开失败需要 'D:/va/ROOT/\xe0\xae\x95.php'(include_path=' .;C:\php5\pear') 在第 0 行的未知

The same page works when file and dir names are in English.当文件和目录名称为英文时,相同的页面有效。 In the same setup, there is no problem using SSI for these pages.在相同的设置中,对这些页面使用 SSI 没有问题。

EDIT编辑

Removed info on url rewriting since it does not seem to be a factor.删除了有关 url 重写的信息,因为它似乎不是一个因素。

When mod_rewrite is removed, the PHP file still does not work.移除 mod_rewrite 后,PHP 文件仍然无法工作。 Works if the file is renamed to a non-UTF name.如果文件重命名为非 UTF 名称,则有效。 However, shtml works even with UTF characters in file and/or path name.但是,shtml 甚至可以在文件和/或路径名中使用 UTF 字符。

I have come across the same problem and done some research and conclude the following.我遇到了同样的问题并做了一些研究并得出以下结论。 This is for php5 on Windows;这是针对 Windows 上的 php5 的; it is probably true on other platforms but I haven't checked.在其他平台上可能是这样,但我没有检查过。

  1. ALL php file system functions (dir, is_dir, is_file, file, filemtime, filesize, file_exists etc) only accept and return file names in ISO-8859-1, irrespective of the default_charset set in the program or ini files.所有 php 文件系统函数(dir、is_dir、is_file、file、filemtime、filesize、file_exists 等)仅接受和返回 ISO-8859-1 中的文件名,而与程序或 ini 文件中设置的 default_charset 无关。

  2. Where a filename contains a unicode character dir->read will return it as the corresponding ISO-8859-1 character if there is one, otherwise it will substitute a question mark.如果文件名包含 unicode 字符 dir->read 会将其作为相应的 ISO-8859-1 字符返回(如果有),否则将替换为问号。

  3. When referencing a file, eg in is_file or file, if you pass in a UTF-8 file name the file will not be found when the name contains any two-byte or more characters.在引用文件时,例如在 is_file 或文件中,如果您传入 UTF-8 文件名,则当该文件名包含任何两个字节或更多字符时,将找不到该文件。 However, is_file(utf8_decode($filename)) etc will work providing the UTF-8 character is representable in ISO-8859-1.但是,如果 UTF-8 字符可以在 ISO-8859-1 中表示,则 is_file(utf8_decode($filename)) 等将起作用。

In other words, PHP5 is not capable of addressing files with multi-byte characters in their names at all.换句话说,PHP5 根本无法处理名称中包含多字节字符的文件。

If a UTF-8 URL with multibyte characters is requested and this corresponds directly to a file, PHP won't be able to open the file because it cannot address it.如果请求了带有多字节字符的 UTF-8 URL 并且这直接对应于文件,则 PHP 将无法打开该文件,因为它无法解决它。

If you simply want pretty URLs in your language the suggestion of using mod_rewrite seems like a good one.如果您只是想要您的语言中漂亮的 URL,那么使用 mod_rewrite 的建议似乎是一个不错的选择。

But if you are storing and retrieving files uploaded and downloaded by users, this problem has to be resolved.但是,如果您正在存储和检索用户上传和下载的文件,则必须解决此问题。 One way is to use an arbitrary (non UTF-8) file name, such as an incrementing number, on the server and index the files in a database or XML file or some such.一种方法是在服务器上使用任意(非 UTF-8)文件名,例如递增的数字,并索引数据库或 XML 文件或类似文件中的文件。 Another way is to store the files in the database itself as a BLOB.另一种方法是将文件作为 BLOB 存储在数据库本身中。 Another way (which is perhaps easier to see what is going on, and not subject to problems if your index gets corrupted) is to encode the filenames yourself - a good technique is to urlencode (sic) all your incoming filenames when storing on the server disk and urldecode them before setting the filename in the mime header for the download.另一种方法(这可能更容易查看发生了什么,并且如果您的索引损坏也不会出现问题)是自己编码文件名 - 一个好的技术是在存储在服务器上时对所有传入的文件名进行 urlencode(原文如此) disk 和 urldecode 它们,然后在 mime header 中设置文件名以供下载。 All even vaguely unusual characters (except %) are then encoded as %nn and so any problems with spaces in file names, cross platform support and pattern matching are largely avoided.然后,所有甚至模糊不寻常的字符(% 除外)都被编码为 %nn,因此在很大程度上避免了文件名中的空格、跨平台支持和模式匹配的任何问题。

  • I know for a fact PHP itself can work with Unicode URLs, because I have tried using Unicode page names in MediaWiki (PHP-based, also runs WikiPedia) and it does work.我知道事实上 PHP 本身可以与 Unicode URL 一起使用,因为我尝试在 MediaWiki 中使用 Unicode 页面名称(基于 PHP,也可以运行 WikiPedia)。 Eg, URLs such as /index.php/Page_name©.例如,/index.php/Page_name© 等 URL。 So PHP can handle it.所以 PHP 可以处理。 But it may be a problem with Apache finding a file where the source file has a UTF-8 name.但它可能是 Apache 找到源文件具有 UTF-8 名称的文件的问题。

  • The PHP.ini setting for character encoding should not be affecting this;字符编码的 PHP.ini 设置不应影响这一点; it is the job of the web server to find a specific resource and then call PHP once it's determined to be a PHP file. web 服务器的工作是查找特定资源,然后在确定为 PHP 文件后调用 PHP。 It will mean that the web server, and the underlying file system itself, have to be able to deal with UTF-8 filenames.这意味着 web 服务器和底层文件系统本身必须能够处理 UTF-8 文件名。

  • Does it work without the mod_rewrite rule?没有 mod_rewrite 规则它可以工作吗? Ie, if you disable the rewrite engine with RewriteEngine off and then request va.in/utf_dir/utf_file.php?即,如果您在关闭 RewriteEngine 的情况下禁用重写引擎,然后请求 va.in/utf_dir/utf_file.php? If so, then it may be a mod_rewrite config issue or a problem with the rule.如果是这样,则可能是 mod_rewrite 配置问题或规则问题。

  • Unicode in URLs may not be properly supported in some browsers when you just type an address in, such as older browsers.当您只输入地址时,某些浏览器(例如旧版浏览器)可能无法正确支持 URL 中的 Unicode。 Older browsers may skip the UTF-8 encoding step.较旧的浏览器可能会跳过 UTF-8 编码步骤。 This should not prevent it from working if you are following a link on a page, where that page is UTF-8 encoded, though.但是,如果您关注页面上的链接,这不应该阻止它工作,该页面是 UTF-8 编码的。

Just because the character set is UTF-8 doesn't mean it supports all the higher characters of Unicode.仅仅因为字符集是 UTF-8 并不意味着它支持 Unicode 的所有更高字符。

Unicode support is one of the major additions coming in PHP 6 and PHP 5 is nutorious for lacking unicode support. Z7F6C02D96265D1D37B1101DE1101541Z支持是Z2FEC392304A5C238DA222847F9B7CZ 6和Z2FEC392392304A5C238DAC138DAC12222222287FFERFEC138DBBFFEC138DBBFFEC138DBBIIous是Z2FEC392304A5C238DAAC138DAAC138DAC138DBER FORENER FORENFENBBFFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFERFIIS。

If your PHP script is generating the link it may be a different issue than if apache is interpreting the url directly and redirecting it.如果您的 PHP 脚本正在生成链接,则与 apache 直接解释 url 并重定向它可能是不同的问题。

No. PHP filenames must be in ASCII, it does not matter how you set up your server PHP5 can't cope so we wait for PHP 6. Within a PHP script you can handle utf-8 filename/url using utf8_decode. No. PHP filenames must be in ASCII, it does not matter how you set up your server PHP5 can't cope so we wait for PHP 6. Within a PHP script you can handle utf-8 filename/url using utf8_decode. You can use a .htaccess and SQL to get around a lot of issues but there is no way to run a unicode filename.您可以使用 .htaccess 和 SQL 来解决很多问题,但无法运行 unicode 文件名。

David Earl's answer is correct.大卫厄尔的回答是正确的。

Use "wfio://" for copy write,etc.使用“wfio://”进行复制写入,etc。

https://github.com/kenjiuno/php-wfio https://github.com/kenjiuno/php-wfio

For folder:对于文件夹:

.htaccess: .htaccess:

php_value auto_prepend_file C:/fix.php

fix.php:修复.php:

$file = $_SERVER['SCRIPT_FILENAME'];
if (!is_readable($file)) {
    $file="wfio://".$file;
                include $file;
                exit;
        }

But better for php use Linux OS但更适合 php 使用 Linux 操作系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM