比较文件路径与PHP,独立操作系统


Comparing filepaths with PHP, OS independently

用PHP比较两个文件路径的最佳方法是什么?

现在,我正在尝试执行preg_match,并且-由于windows DS会破坏regex-通过str_replace(DIRECTORY_SEPARATOR, '/', $path); 运行这两种模式

我建议您不要比较完整的文件路径,而只比较从网站根开始的相对路径。

所以就这样做吧:

$path = str_replace($_SERVER['DOCUMENT_ROOT'], "", $path); // this will cut your website root
if (DIRECTORY_SEPARATOR != "/")
   $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);   // replace non unix slashes
if (preg_match($yourpattern, $path))
{
    // do something...
}