Mac 和 Windows 中的绝对路径


Absolute paths in Mac and Windows

我有一个PHP程序,它使用带有绝对路径的dom->save()。

Applications/XAMPP/xamppfiles/htdocs/feed/ultrafeed.xml

我将在Mac和Windows机器上使用它,那么有没有办法创建一个相对或独立于用户平台的路径?

是,

使用 realpath()

试试这个,例如你有一个相对路径:

$path = "feed/ultrafeed.xml";
$path = realpath($path);
echo $path;

这将使您在Windows
C:'real'path'feed'ultrafeed.xml/real/path/feed/ultrafeed.xml在Unix上。

$_SERVER ['DOCUMENT_ROOT'] .DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR .'to'.DIRECTORY_SEPARATOR. 'file';`

如果您知道它相对于当前文件的位置,请使用__DIR__而不是 f $_SERVER