File_exists函数无法通过符号链接目录的相对路径正常工作


File_exists function do not work properly with relative paths through symlink directory

我创建了重现此问题的简单代码:

<?php
exec('mkdir foo');
exec('mkdir foo/bar');
exec('touch foo/bar/example.txt');
exec('ln -s foo/bar symlink');
exec('mkdir not_symlink');
$dir1 = 'symlink/../foo/bar/example.txt';
$dir2 = 'not_symlink/../foo/bar/example.txt';
$dir3 = 'symlink/example.txt';
$dir4 = 'foo/bar/example.txt';
$dir5 = 'foo/bar/../bar/example.txt';
$dir6 = 'symlink';
var_dump(file_exists($dir1)); // false ???
var_dump(file_exists($dir2)); // true
var_dump(file_exists($dir3)); // true
var_dump(file_exists($dir4)); // true
var_dump(file_exists($dir5)); // true
var_dump(file_exists($dir6)); // true

为什么file_exists返回 false 表示$dir1?我在 php 文档中找不到这种行为的任何解释。

我在 Ubuntu 14.04 64 位上用 PHP 5.5.9 测试了这段代码。

exec('ln -s foo/bar symlink');
$dir1 = 'symlink/../foo/bar/example.txt';

解析符号链接后,$dir1 'foo/bar/../foo/bar/example.txt' 。或者,简单的'foo/foo/bar/example.txt'.

而这条路并不存在。

您尝试解决的路径是错误的。

$ readlink -f symlink/../foo
/home/myuser/foo/foo

添加../

$ readlink -f symlink/../../foo/bar/example.txt
/home/myuser/foo/bar/example.txt