PHP dirname returns symlink path

前端 未结 5 604
礼貌的吻别
礼貌的吻别 2021-01-02 08:40

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

If I echo dirname(dirname(\\__FILE__)), it returns

相关标签:
5条回答
  • 2021-01-02 08:54

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

    Edit : readlink seems to be a better answer :)

    0 讨论(0)
  • 2021-01-02 08:56
    <?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

    0 讨论(0)
  • 2021-01-02 08:57

    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'

    0 讨论(0)
  • 2021-01-02 09:03

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

    <?php 
        echo readlink(dirname(__FILE__));
    ?>
    
    0 讨论(0)
  • 2021-01-02 09:05

    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));
    }
    
    0 讨论(0)
提交回复
热议问题