Get parent directory of running script

后端 未结 13 2051
攒了一身酷
攒了一身酷 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 04:55

    If I properly understood your question, supposing your running script is

    /relative/path/to/script/index.php
    

    This would give you the parent directory of your running script relative to the document www:

    $parent_dir = dirname(dirname($_SERVER['SCRIPT_NAME'])) . '/';
    //$parent_dir will be '/relative/path/to/'
    

    If you want the parent directory of your running script relative to server root:

    $parent_dir = dirname(dirname($_SERVER['SCRIPT_FILENAME'])) . '/';
    //$parent_dir will be '/root/some/path/relative/path/to/'
    

提交回复
热议问题