PHP dirname returns symlink path

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

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

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

Using an example:

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

example.com/hello_world.php

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

Returns: '/one/directory'

Expected Results: '/two/directory'

+6
source share
5 answers

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

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

 if (is_link($link)) { echo(readlink($link)); } 
+8
source

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

 <?php echo readlink(dirname(__FILE__)); ?> 
+6
source
 <?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

+1
source

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

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

 <?php echo SymlinkDetective::detectPath(dirname(dirname(__FILE__))); 

will return '/two/directory'

+1
source

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

Edit: readlink seems to be the best answer :)

-1
source

Source: https://habr.com/ru/post/926193/


All Articles