Get parent directory of running script

后端 未结 13 2001
攒了一身酷
攒了一身酷 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.

    0 讨论(0)
  • 2020-11-30 04:52

    Got it myself, it's a bit kludgy but it works:

    substr(dirname($_SERVER['SCRIPT_NAME']), 0, strrpos(dirname($_SERVER['SCRIPT_NAME']), '/') + 1)
    

    So if I have /path/to/folder/index.php, this results in /path/to/.

    0 讨论(0)
  • 2020-11-30 04:55

    If your script is located in /var/www/dir/index.php then the following would return:

    dirname(__FILE__); // /var/www/dir
    

    or

    dirname( dirname(__FILE__) ); // /var/www
    

    Edit

    This is a technique used in many frameworks to determine relative paths from the app_root.

    File structure:

     /var/
          www/
              index.php
              subdir/
                     library.php
    

    index.php is my dispatcher/boostrap file that all requests are routed to:

    define(ROOT_PATH, dirname(__FILE__) ); // /var/www
    

    library.php is some file located an extra directory down and I need to determine the path relative to the app root (/var/www/).

    $path_current = dirname( __FILE__ ); // /var/www/subdir
    $path_relative = str_replace(ROOT_PATH, '', $path_current); // /subdir
    

    There's probably a better way to calculate the relative path then str_replace() but you get the idea.

    0 讨论(0)
  • 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/'
    
    0 讨论(0)
  • 2020-11-30 04:55

    This is a function that I use. Created it once so I always have this functionality:

    function getDir(){
        $directory = dirname(__FILE__);
        $directory = explode("/",$directory);
        $findTarget = 0;
        $targetPath = "";
        foreach($directory as $dir){
            if($findTarget == 1){
                $targetPath = "".$targetPath."/".$dir."";
            }
            if($dir == "public_html"){
                $findTarget = 1;
            }
        }
        return "http://www.".$_SERVER['SERVER_NAME']."".$targetPath."";
    }
    
    0 讨论(0)
  • 2020-11-30 05:01

    Fugly, but this will do it:

    substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'],basename($_SERVER['SCRIPT_NAME'])))
    
    0 讨论(0)
提交回复
热议问题