Get parent directory of running script

后端 未结 13 2002
攒了一身酷
攒了一身酷 2020-11-30 04:20

In PHP, what would be the cleanest way to get the parent directory of the current running script relative to the www root? Assume I have:

$_         


        
相关标签:
13条回答
  • 2020-11-30 05:01

    I Hope this will help you.

    echo getcwd().'<br>'; // getcwd() will return current working directory
    echo dirname(getcwd(),1).'<br>';
    echo dirname(getcwd(),2).'<br>';
    echo dirname(getcwd(),3).'<br>';
    

    Output :

    C:\wamp64\www\public_html\step
    C:\wamp64\www\public_html
    C:\wamp64\www
    C:\wamp64
    
    0 讨论(0)
  • 2020-11-30 05:05

    I hope this will help

    function get_directory(){
        $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
        $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
        $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
    return $protocol . "://" . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']);
    }
    define("ROOT_PATH", get_directory()."/" );
    echo ROOT_PATH;
    
    0 讨论(0)
  • 2020-11-30 05:06

    As of PHP 5.3.0 you can use __DIR__ for this purpose.

    The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__ FILE__).

    See PHP Magic constants.

    C:\www>php --version
    PHP 5.5.6 (cli) (built: Nov 12 2013 11:33:44)
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    
    C:\www>php -r "echo __DIR__;"
    C:\www
    
    0 讨论(0)
  • 2020-11-30 05:06

    To get the parentdir of the current script.

    $parent_dir = dirname(__DIR__);
    
    0 讨论(0)
  • 2020-11-30 05:12
    $dir = dirname($file) . DIRECTORY_SEPARATOR;
    
    0 讨论(0)
  • 2020-11-30 05:12

    This is also a possible solution

    $relative = '/relative/path/to/script/';
    $absolute = __DIR__. '/../' .$relative;
    
    0 讨论(0)
提交回复
热议问题