file_exists() 用于文件名中包含 (&) 的文件

5 php algorithm filenames character file-exists

$filename = "Sara & I.mp3";
if (file_exists($filename)) {
 ...
}
Run Code Online (Sandbox Code Playgroud)

如果文件名有“ &” PHP 不会返回 truefile_exists

如何&在 $filename 中转义“ ”才能file_exists工作?

已经尝试过urlecode(), urlrawencode(), htmlentities(), 并用反斜杠 (\&) 转义 '&'...不行

附:这是在运行 RedHat5 的 Linux 系统上

提前致谢

Cha*_* Ma 2

该脚本似乎对我有用。

确保您要检查的文件位于运行脚本的目录中。

使用 getcwd() 查看脚本的运行位置。

例如,如果您的脚本位于 my/scripts/script.php 中,但如果它被 my/other/scripts/index.php 中的另一个脚本包含并调用,那么您的脚本实际上将在 my/other/scripts 目录中运行,而不是在如您所料的 my/scripts 目录