假设我有一个从
'/one/directory/'
到 '/two/directory/'
的符号链接。
如果我
echo dirname(dirname(\__FILE__))
,它会返回'/one/directory/'
。
最好的退货方式是什么
'/two/directory'
?
用法示例:
Vhost
'example.com'
指向`'/two/directory'
example.com/hello_world.php
<?php echo dirname(dirname(__FILE__)); ?>
退货:
'/one/directory'
预期结果:
'/two/directory'
使用
readlink
功能? http://php.net/manual/en/function.readlink.php
您可以使用
is_link
检查它是否是符号链接: http://php.net/manual/en/function.is-link.php
if (is_link($link)) {
echo(readlink($link));
}
使用 readlink($path) 读取符号链接的目标。
<?php
echo readlink(dirname(__FILE__));
?>
<?php
function getRealFile($path) {
return is_link($path) ? readlink($path) : $path;
}
$path = getRealFile(dirname(__FILE__));
文档:
http://php.net/manual/en/function.is-link.php
http://php.net/manual/en/function.readlink.php
返回“/two/directory”的最佳方法是什么?
我编写了一个包来执行此操作:https://github.com/ological-and/symlink-Detective
它允许您执行此操作:
<?php
echo SymlinkDetective::detectPath(dirname(dirname(__FILE__)));
会回来
'/two/directory'
也许使用 realpath() ? http://php.net/manual/en/function.realpath.php
编辑:readlink 似乎是一个更好的答案:)