php 获取运行脚本的父目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1882044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:09:38  来源:igfitidea点击:

Get parent directory of running script

phpdirectory

提问by Tatu Ulmanen

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

在 PHP 中,获取当前运行脚本相对于 www 根目录的目录的最干净方法是什么?假设我有:

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

Or just:

要不就:

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

And I need to get /relative/path/to/with slashes properly inserted. What would you suggest? A one liner is preferred.

我需要/relative/path/to/正确插入斜线。你有什么建议?优选单衬。

EDIT

编辑

I need to get a path relative to the www root, dirname(__FILE__)gives me an absolute path in the filesystem so that won't work. $_SERVER['SCRIPT_NAME']on the other hand 'starts' at the www root.

我需要获得一个相对于 www 根dirname(__FILE__)的路径,在文件系统中给我一个绝对路径,这样就行不通了。$_SERVER['SCRIPT_NAME']另一方面,在 www 根“开始”。

回答by Mike B

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

如果您的脚本位于,/var/www/dir/index.php则将返回以下内容:

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.

这是许多框架中使用的一种技术,用于确定来自 app_root 的相对路径。

File structure:

文件结构:

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

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

index.php 是我的调度程序/boostrap 文件,所有请求都路由到:

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/).

library.php 是位于额外目录下的某个文件,我需要确定相对于应用程序根目录 (/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.

可能有更好的方法来计算相对路径,str_replace()但您明白了。

回答by Charlie Vieillard

As of PHP 5.3.0 you can use __DIR__for this purpose.

从 PHP 5.3.0 开始,您可以__DIR__用于此目的。

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

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

See PHP Magic constants.

请参阅PHP 魔术常量

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

回答by hans

To get the parentdir of the current script.

获取当前脚本的父目录。

$parent_dir = dirname(__DIR__);

回答by Marco Demaio

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:

这将为您提供相对于文档 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/'

回答by Brad

Fugly, but this will do it:

Fugly,但这会做到:

substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'],basename($_SERVER['SCRIPT_NAME'])))

回答by Vignesh KM

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

回答by Alex

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

这是我使用的,因为我没有运行 > 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.

按照@mike b 为父目录的建议,将 dirname 与 file 一起使用,只需使用一次该语法即可找到当前目录。

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

请注意,此函数仅返回 NAME,之后必须添加斜杠。

回答by Jordan Ryan Moore

$dir = dirname($file) . DIRECTORY_SEPARATOR;

回答by cestar

Try this. Works on both windows or linux server..

尝试这个。适用于 windows 或 linux 服务器..

str_replace('\\','/',dirname(dirname(__FILE__)))

str_replace('\\','/',dirname(dirname(__FILE__)))

回答by John

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."";
}