Warning: unknown: failed to open stream: no such file or directory in unknown on line 0
Fatal error: unknown: failed opening required 'e:/ttttt/中文/index.php' (include_path='.;c:php5pear') in unknown on line 0
明明文件存在,include_path 配置也正確,卻報「無法打開流:沒有該文件或目錄」。
PHP 核心在 Windows 下對中文路徑的支持存在缺陷。 當文件路徑包含中文字符時,PHP 內部將路徑編碼傳遞給系統 API 時可能出現編碼轉換錯誤(GBK/UTF-8 混淆),導致系統找不到實際路徑。這是 PHP 歷史遺留問題,非 include_path 配置問題。
原則:項目代碼、資源文件、上傳目錄的所有層級路徑,嚴禁出現中文字符。
// 錯誤結構
/e:/project/中文目錄/用戶模塊/index.php
// 正確結構
/e:/project/chinese_dir/user_module/index.php
// 或使用拼音/英文單詞
/e:/project/zh_cn/user/index.php
僅針對 Apache 2.2+ 配置的虛擬目錄(Alias/DocumentRoot),若將 httpd.conf 保存為 UTF-8 無 BOM 編碼,則可使用中文目錄名。但 PHP 腳本內部的 include/require/fopen 等文件系統操作仍不支持中文路徑。
[a-z0-9_-])。httpd.conf、php.ini、.env 等均保存為 UTF-8 無 BOM。<?php
// 批量重命名中文目錄/文件為拼音(需安裝 overtrue/pinyin)
require 'vendor/autoload.php';
use Overtrue\Pinyin\Pinyin;
$pinyin = new Pinyin();
$root = '/path/to/project';
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST // 先處理子項,再處理父目錄
);
foreach ($iterator as $file) {
$basename = $file->getBasename();
// 簡單判斷是否含中文
if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $basename)) {
$newName = $pinyin->permalink($basename, '_'); // 中文轉拼音下劃線
$newPath = $file->getPath() . DIRECTORY_SEPARATOR . $newName;
if (!file_exists($newPath)) {
rename($file->getPathname(), $newPath);
echo "Renamed: {$basename} -> {$newName}\n";
}
}
}
?>
PHP 中文路徑問題已存在十餘年,社區共識是「規避而非修復」。遵循「純 ASCII 路徑」規範,能省去無數排查工時,也是專業項目工程化的基本素養。