|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50366 realpath() doesn't correctly resolve symlink
Submitted: 2009-12-02 22:07 UTC Modified: 2010-12-20 09:12 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: jerico dot dev at gmail dot com Assigned:
Status: Open Package: Filesystem function related
PHP Version: 5.2.11 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
9 + 18 = ?
Subscribe to this entry?

 [2009-12-02 22:07 UTC] jerico dot dev at gmail dot com
realpath() doesn't resolve symlinks correctly when followed by /..

Reproduce code:
echo realpath('/home/user/test/app/lib/symlink/tests/../../..');

where "symlink" points to /home/user/test/something

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-03 08:13 UTC]
"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/ ?

See this:

# php -r  'echo realpath("/home/jani/test/app/lib/symlink/tests/"), "\n";'

# php -r  'echo realpath("/home/jani/test/app/lib/symlink/tests/../../.."), "\n";'

 [2009-12-03 18:28 UTC] jerico dot dev at gmail dot com
Hi Jani,

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

echo getcwd()."\n";

To see what I mean.

Also see and
which request very similar features.

The only way I know to achieve 1) would be going to OS-level (e.g. using `cd ...` or 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.
 [2010-12-20 09:12 UTC]
-Package: Feature/Change Request +Package: Filesystem function related
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 14:01:30 2024 UTC