go to bug id or search bugs for
realpath() doesn't resolve symlinks correctly when followed by /..
where "symlink" points to /home/user/test/something
Add a Patch
Add a Pull Request
"realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path . and return the canonicalized absolute pathname. "
And since it first resolves to /home/user/test/something/tests from where you go down 3 levels, of course you get /home/user/ ?
# php -r 'echo realpath("/home/jani/test/app/lib/symlink/tests/"), "\n";'
# php -r 'echo realpath("/home/jani/test/app/lib/symlink/tests/../../.."), "\n";'
thanks a lot for looking into this.
I think the problem is that what is "logical" depends on what your use case is. There are two different approaches to resolving paths to canonical (physical) locations with symlinks:
1) Resolving .. first, then symlinks. This is AFAIK the default for the *nix command line (try cd'ing or ls'ing the example location and you'll see what I mean).
2) Resolving symlinks first, then .. There is one tool on Linux (that I know of) that behaves like this: readlink -e
I agree with you that there ARE use cases where you want 2). I've changed the bug category to "feature request" to reflect this. Sorry for filing a bug in the first place. :-(
There are many use cases, though, where realpath()/chdir() behavior is NOT useful (otherwise the OS would probably have chosen realpath/chdir logic as it's default ;-) ). The problem is that AFAIK PHP currently has no way of doing 1). Try
To see what I mean.
which request very similar features.
The only way I know to achieve 1) would be going to OS-level (e.g. using `cd ...` or s.th. like that) which is awkward and not portable.
If you know a portable way to map a relative or absolute path with symlinks to it's physical location "OS style" in PHP then it would be extremely useful for me (and probably others) if you could share your knowledge. It's entirely possible that I miss something.
If you feel extremely helpful ;-) then you also might extend the PHP documentation of realpath()/chdir() and make it clear that both treat symlinks the same way as readlink -e does. There already is a user comment for chdir() saying that but I think it'd be worth mentioning at a more prominent location.