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'

Leri
  • 12,367
  • 7
  • 43
  • 60
adamstrawson
  • 115
  • 1
  • 2
  • 8
  • 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' – FantomX1 Aug 07 '20 at 14:08

5 Answers5

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));
}
manavo
  • 1,839
  • 2
  • 14
  • 17
  • I went with this example since I wanted to check if it was a Symlink first as not all environments use them. – adamstrawson Sep 26 '12 at 07:55
  • Thanks for the comment @Erwinus, will be good for people to know! Unfortunately I don't have a Windows 7 machine to test this! – manavo Jan 20 '16 at 10:44
  • 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. – Bigue Nique Nov 18 '17 at 08:27
7

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

<?php 
    echo readlink(dirname(__FILE__));
?>
Starx
  • 77,474
  • 47
  • 185
  • 261
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
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

Eugene Naydenov
  • 7,165
  • 2
  • 25
  • 43
  • 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 example – FantomX1 Aug 07 '20 at 14:20
2

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

Use the https://github.com/logical-and/symlink-detective and

<?php 

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

will return '/two/directory'

And
  • 97
  • 5
  • 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 docs – FantomX1 Aug 07 '20 at 14:18
1

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

Edit : readlink seems to be a better answer :)

Fry_95
  • 241
  • 1
  • 6