Get parent directory of running script

后端 未结 13 2085
攒了一身酷
攒了一身酷 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:52

    Here is what I use since I am not running > 5.2

    function getCurrentOrParentDirectory($type='current')
    {
        if ($type == 'current') {
            $path = dirname(__FILE__);  
        } else {
            $path = dirname(dirname(__FILE__));
        }
        $position = strrpos($path, '/') + 1;
        return substr($path, $position);
    }
    

    Double dirname with file as suggested by @mike b for the parent directory, and current directory is found by just using that syntax once.

    Note this function only returns the NAME, slashes have to be added afterwards.

提交回复
热议问题