【发布时间】:2023-04-09 01:43:01
【问题描述】:
这个问题应该很简单......为了重现这个场景,我设置了这个结构:
Root
|----/test/abc.txt
|----index.php
index.php
$root = __DIR__;
echo is_file($root . "/test/abc.txt") ? "true" : "false";
echo is_file($root . "//test/abc.txt") ? "true" : "false";
echo is_file($root . "///test/abc.txt") ? "true" : "false";
echo is_dir($root . "/test") ? "true" : "false";
echo is_dir($root . "//test") ? "true" : "false";
echo is_dir($root . "///test") ? "true" : "false";
echo file_exists($root . "/test") ? "true" : "false";
echo file_exists($root . "//test") ? "true" : "false";
echo file_exists($root . "///test") ? "true" : "false";
输出: is_file()
true
true
true
输出: is_dir()
true
true
true
输出: file_exists()
true
true
true
前导斜杠似乎被“忽略”了,无论如何它仍然可以找到文件位置。我以为斜杠表示文件夹目录,但我似乎错了。这里发生了什么?
这可能是环境设置问题,但我还没有设置 linux 来测试。
我目前的环境是windows OS,XAMPP。
更新:
这似乎也适用于is_dir() 和file_exists() 函数。
【问题讨论】:
-
只有 MS-Windows 操作系统使用黑色斜线 (\) 作为目录边界标记。其他操作系统(Linux、Unix、MacOS)使用正斜杠(
/)。这就是为什么通常在“互联网”和“网络”(无论这实际上意味着什么)中,正斜杠是公认的。像 PHP 这样的语言试图接受这两者并理解它。但如果有疑问,请使用正斜杠。所以对我来说,你的测试脚本的输出在所有方面都有意义。双斜杠被视为单斜杠,尾部斜杠表示目录,而不是文件。 -
@arkascha 抱歉我打错了,反斜杠应该是“正斜杠”,因为我在代码中使用了正斜杠 -_-“”,但你说的是
Double slashes are treated like single slashes,从技术上讲是数百正斜杠对存在的路径无关紧要?? -
路径参数中被解释为单个正斜杠,是的。
-
与
/./././相同,因为单点 (.) 代表目录本身。 -
哦,顺便说一句,你可能想看看
realpath()函数:php.net/manual/en/function.realpath.php
标签: php file operating-system