PHP dirname returns symlink path
Asked Answered
O

5

9

Say I have a symlink from '/one/directory/' to '/two/directory/'.

If I echo dirname(dirname(\__FILE__)), it returns '/one/directory/'.

What is the best method to return '/two/directory'?

Example usage:

Vhost 'example.com' pointing to `'/two/directory'

example.com/hello_world.php

<?php 
    echo dirname(dirname(__FILE__));
?>

Returns: '/one/directory'

Expected results: '/two/directory'

Orndorff answered 25/9, 2012 at 9:44 Comment(1)
Your question is not quite clear, what is the symlink and what is the real directory? According the documentation, symlinks are automatically resolved with FILE from old php 4.0.2, so it would be reasonable to think, that you complain that you consider symlink being the 'two' directory and you complain the FILE returning the original = symlink resolved directory 'one'. Though almost all the answer here reply as if the original directory was two, the symlink one, and the PHP did not behave as they write in documentation, that the FILE var would return not symlinked resolved 'one'Sabah
T
10

Use the readlink function? http://php.net/manual/en/function.readlink.php

You can check if it is a symlink with is_link: http://php.net/manual/en/function.is-link.php

if (is_link($link)) {
    echo(readlink($link));
}
Taboo answered 25/9, 2012 at 9:47 Comment(3)
I went with this example since I wanted to check if it was a Symlink first as not all environments use them.Orndorff
Thanks for the comment @Erwinus, will be good for people to know! Unfortunately I don't have a Windows 7 machine to test this!Taboo
Update: now works on Windows, along with link(), symlink() and linkinfo() - since PHP 5.3.0 according to the manual. I confirm it does work on Windows 7.Glaswegian
C
7

Use readlink($path) to read the target of symbolic link.

<?php 
    echo readlink(dirname(__FILE__));
?>
Chas answered 25/9, 2012 at 9:46 Comment(5)
This doesn't seem to work for me - it doesn't return any output.Orndorff
lorga - Weird, i'll have a play. I'm wondering it it's something with vhosts and how it's handling symlinks. Thanks!Orndorff
No matter what I pass to it, it returns Warning: readlink(): Invalid argument :(Orndorff
what are you trying to pass ?Chas
I've worked it out, but it seems to be backwards. I have to pass '/two/directory' for it to work, when I want to pass '/one/directory' to get '/two/directory'Orndorff
Q
3
<?php

function getRealFile($path) {
    return is_link($path) ? readlink($path) : $path;
}

$path = getRealFile(dirname(__FILE__));

Documentation:

http://php.net/manual/en/function.is-link.php
http://php.net/manual/en/function.readlink.php

Qualitative answered 25/9, 2012 at 9:49 Comment(1)
According to php docs version 4.0.2 above, FILE is always automatically symlink resolved, so I don't know why you suggest a function for something, what is implicitly done, and the asker asks for the opposite. Analogy to how it sounds : ` function getZero(number) { return number==0 ? number * 0 : number; } ` and then posting a link to math forum mentioning that the result of multiplication by 0 is zero Your answer might be an answer, but not for this question, when the supplied parameter is FILE as in your exampleSabah
A
2

What is the best method to return '/two/directory'?

I wrote a package to do this: https://github.com/logical-and/symlink-detective

It allows you to do this:

<?php 

echo SymlinkDetective::detectPath(dirname(dirname(__FILE__)));

will return '/two/directory'

Augsburg answered 5/11, 2016 at 8:49 Comment(1)
Seems like he wants to get a symlink path = symlink unresolved path, and you are suggesting him the opposite in other words - what he already has, as there is no way in php not to get non symlink resolved path with FILE according to docsSabah
K
1

Maybe with realpath() ? http://php.net/manual/en/function.realpath.php

Edit : readlink seems to be a better answer :)

Kissiah answered 25/9, 2012 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.