代码之家  ›  专栏  ›  技术社区  ›  Tatu Ulmanen

获取正在运行的脚本的父目录

  •  62
  • Tatu Ulmanen  · 技术社区  · 15 年前

    父母亲 相对于www根目录的当前运行脚本的目录?假设我有:

    $_SERVER['SCRIPT_NAME'] == '/relative/path/to/script/index.php'
    

    $something_else == '/relative/path/to/script/'
    

    我需要得到 /relative/path/to/ 正确插入斜杠。你有什么建议?最好是一个衬里。

    编辑

    我需要得到一个相对于www根的路径, dirname(__FILE__) 在文件系统中为我提供了一个绝对路径,这样就不起作用了。 $_SERVER['SCRIPT_NAME'] 另一方面,从www根目录“开始”。

    13 回复  |  直到 15 年前
        1
  •  158
  •   Mike B    15 年前

    如果您的脚本位于 /var/www/dir/index.php

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

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

    编辑

    这是一种在许多框架中用于确定app_根的相对路径的技术。

    文件结构:

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

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

    library.php是位于一个额外目录下的某个文件,我需要确定相对于app root(/var/www/)的路径。

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

    也许有更好的方法来计算相对路径 str_replace() 但你明白了。

        2
  •  13
  •   Charlie Vieillard    11 年前

    从PHP5.3.0开始,您可以使用 __DIR__

    文件的目录。如果在包含中使用,则为 返回包含的文件。这相当于 目录名(文件名)。

    看见 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
    
        3
  •  8
  •   Marco Demaio    12 年前

    如果我正确理解了你的问题,假设你的运行脚本是

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

    这将为您提供运行脚本相对于文档www的父目录:

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

    $parent_dir = dirname(dirname($_SERVER['SCRIPT_FILENAME'])) . '/';
    //$parent_dir will be '/root/some/path/relative/path/to/'
    
        4
  •  8
  •   hans    11 年前

    $parent_dir = dirname(__DIR__);
    
        5
  •  5
  •   Sliq    11 年前

    Fugly,但这可以做到:

    substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'],basename($_SERVER['SCRIPT_NAME'])))
    
        6
  •  4
  •   Vignesh KM    7 年前

    我希望这对你有帮助。

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

    输出:

    C:\wamp64\www\public_html\step
    C:\wamp64\www\public_html
    C:\wamp64\www
    C:\wamp64
    
        7
  •  2
  •   Jordan Ryan Moore    15 年前
    $dir = dirname($file) . DIRECTORY_SEPARATOR;
    
        8
  •  2
  •   Alex    11 年前

    以下是我不跑步时使用的内容>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);
    }
    

    双击dirname和@mike b建议的父目录的文件,只需使用该语法一次即可找到当前目录。

        9
  •  1
  •   cestar    11 年前

    试试这个。 可在windows或linux服务器上运行。。

    str\u replace('\\','/',dirname(dirname(\uuu FILE\uuu)))

        10
  •  1
  •   John    8 年前

    这是我使用的一个函数。创建一次,因此我始终具有以下功能:

    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."";
    }
    
        11
  •  0
  •   collidoscope    9 年前

    $relative = '/relative/path/to/script/';
    $absolute = __DIR__. '/../' .$relative;
    
        12
  •  0
  •   Goodhope Kudakwashe Dhliwayo    7 年前

    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;
    
        13
  •  -7
  •   Tatu Ulmanen    15 年前

    我自己买的,有点麻烦,但它能工作:

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

    所以如果我有 /path/to/folder/index.php /path/to/