Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP dirname returns symlink path

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'

like image 575
adamstrawson Avatar asked Sep 25 '12 09:09

adamstrawson


1 Answers

<?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

like image 176
Eugene Naydenov Avatar answered Sep 30 '22 17:09

Eugene Naydenov