如何从 PHP 获取我的 URL 的“应用程序根”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2311983/
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 05:59:48  来源:igfitidea点击:

How can I get the "application root" of my URL from PHP?

php

提问by scottm

If my URL is http://www.server.com/myapp/stuff/to/seeand my directory structure on disk is htdocs/myapp/*, how can I extract the /myapppart? Also, what if there was no application folder and the application root was just '/'? Is there a predefined variable for getting that value?

如果我的 URL 是http://www.server.com/myapp/stuff/to/see并且我在磁盘上的目录结构是htdocs/myapp/*,我该如何提取该/myapp部分?另外,如果没有应用程序文件夹并且应用程序根目录只是“/”怎么办?是否有用于获取该值的预定义变量?

What I want is a function that is able to trim /myappoff of the request URI, so that I'm only left with /stuff/to/see. AND, if I were to move the application to the server document root, have it determine that as well (so that /stuff/to/seeis just returned as /stuff/to/see)

我想要的是一个能够修剪/myapp请求 URI的函数,这样我就只剩下/stuff/to/see. 并且,如果我要将应用程序移动到服务器文档根目录,也请确定它(因此它/stuff/to/see只是作为 返回/stuff/to/see

My directory structure is this:

我的目录结构是这样的:

application
  |-config
    |-config.inc.php
library
  |-autoload.class.php
public
  |-css
  |-img
index.php

So, from index.php, I want to know how to get what I asked above.

所以,从 index.php,我想知道如何得到我上面问的内容。

采纳答案by Pekka

For the web root, there is DOCUMENT_ROOTas pointed out in several answers. For the application root, there is no way for the system to tell which folder in your application is the root folder. You will have to set that manually.

对于web rootDOCUMENT_ROOT正如几个答案中所指出的那样。对于应用程序 root,系统无法判断应用程序中的哪个文件夹是根文件夹。您必须手动设置。

Most applications define an application root using dirname(__FILE__);or a config setting and store that in a constant that is available throughout the application.

大多数应用程序使用dirname(__FILE__);或 配置设置定义应用程序根并将其存储在整个应用程序可用的常量中。

回答by poke

When you access a page using http://www.server.com/myapp/stuff/to/see, and your webserver's document root is at /something/htdocs/, then the current localpath is /something/htdocs/myapp/stuff/to/see.

当您使用 访问页面http://www.server.com/myapp/stuff/to/see,并且您的网络服务器的文档根目录为 时/something/htdocs/,当前local路径为/something/htdocs/myapp/stuff/to/see

There is no definite solution to get /something/htdocs/myapp/as a result now, because there is no clear definition whichpath you should get. When you have some rule for that, tell us, and we might be able to come up with something, but without computation, the only paths you can see are:

现在没有明确的解决方案可以得到/something/htdocs/myapp/结果,因为没有明确的定义应该得到哪条路径。当您对此有一些规则时,请告诉我们,我们也许可以想出一些办法,但无需计算,您只能看到以下路径:

  • http://www.server.com/myapp/stuff/to/see
    $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
  • /something/htdocs/myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_FILENAME']
  • /something/htdocs/
    $_SERVER['DOCUMENT_ROOT']
  • /myapp/stuff/to/see
    $_SERVER['REQUEST_URI']
  • /myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_NAME']and $_SERVER['PHP_SELF']
  • http://www.server.com/myapp/stuff/to/see
    $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
  • /something/htdocs/myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_FILENAME']
  • /something/htdocs/
    $_SERVER['DOCUMENT_ROOT']
  • /myapp/stuff/to/see
    $_SERVER['REQUEST_URI']
  • /myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_NAME']$_SERVER['PHP_SELF']

回答by Andrew Moore

<?php
define('ABSPATH', dirname(__FILE__));

Put the following code in a file located in the root folder of your application and include it on every page load.

将以下代码放在位于应用程序根文件夹中的文件中,并将其包含在每个页面加载中。

Then, you can simply always do $path = ABSPATH . '/path/to/file.php';regardless of if your local copy is in a sub-directory folderor not.

然后,$path = ABSPATH . '/path/to/file.php';无论您的本地副本是否在子目录中,您都可以随时执行folder



If your application already has a file which is included on every page load, you can simply drop the code above in that file and it will work.

如果您的应用程序已经有一个包含在每个页面加载中的文件,您只需将上面的代码放在该文件中,它就会起作用。

Just note that you may have to add additional dirname()calls depending on where that file is located. Add one for each directory you pass from the root of your webapp.

请注意,您可能dirname()需要根据该文件所在的位置添加其他调用。为您从 web 应用程序的根目录传递的每个目录添加一个。

For example, if your webapp is located in /webapp/and your "global include" is located in /webapp/includes/framework/init.php, then the above code needs to be modified as such:

例如,如果您的 webapp 位于/webapp/并且您的“全局包含”位于 中/webapp/includes/framework/init.php,则需要将上述代码修改为:

define('ABSPATH', dirname(dirname(dirname(__FILE__))));

ie.: 2 additional dirname()calls due to two additional folders from the webapp root (includes/framework)

即:dirname()由于来自 webapp 根 ( includes/framework) 的两个额外文件夹而导致的2 个额外调用



Clarification

澄清

The code above is meant to be in one file, and one file only in your web application. That file needs to be included on each page load.

上面的代码应该放在一个文件中,并且一个文件只存在于您的 Web 应用程序中。该文件需要包含在每个页面加载中。

If you already have a file which is included before any processing (such as a configuration file or other), you may copy and paste that code in that file.

如果您已经有一个在任何处理之前包含的文件(例如配置文件或其他文件),您可以将该代码复制并粘贴到该文件中。

The number of dirname()calls depends on how deep the file you copied and pasted the code in is relative to the root directory of your web application. For the examples above, assume the root of your web application is represented by ~.

dirname()调用取决于你有多深的文件复制并粘贴在相对于你的web应用程序的根目录中的代码。对于上面的示例,假设您的 Web 应用程序的根由 表示~

If you copy-paste my code into ~/abspath.php, then you need one dirname()call.

如果您将我的代码复制粘贴到 中~/abspath.php,那么您需要一个dirname()电话。

If you copy-paste my code into ~/includes/abspath.php, then you need two dirname()calls.

如果您将我的代码复制粘贴到 中~/includes/abspath.php,则需要两次dirname()调用。

If you copy-paste my code into ~/includes/config/abspath.php, then you need three dirname()calls. Now let's just say that's its final location.

如果您将我的代码复制粘贴到 中~/includes/config/abspath.php,则需要三个dirname()调用。现在让我们说这是它的最终位置。

In ~/index.php, you do the following:

在 中~/index.php,您执行以下操作:

<?php
require_once('includes/config/abspath.php');

and you have access to ABSPATH.

并且您可以访问ABSPATH.

In ~/dir/someOtherPage.phpyou do the following:

~/dir/someOtherPage.php您执行以下操作:

<?php
require_once('../includes/config/abspath.php');

and you have access to ABSPATH.

并且您可以访问ABSPATH.

This is why I'm saying that if you already have a file which is included on each page load, its simpler just to drop the above code in it. Just make sure you modify the amount of dirname()calls accordingly. Again, this code is meant to be in ONLY ONE FILE.

这就是为什么我说如果您已经有一个包含在每个页面加载中的文件,只需将上述代码放入其中就更简单了。只需确保相应地修改dirname()调用量即可。同样,此代码仅存在于一个文件中。



Then do get the URL path, its a simple matter of removing the DOCUMENT_ROOTFROM ABSPATH:

然后获取 URL 路径,删除DOCUMENT_ROOTFROM很简单ABSPATH

$docRoot = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
define('RELADDR', substr(ABSPATH, strlen($docRoot));

回答by widyakumara

$_SERVER['DOCUMENT_ROOT']will give you the path to htdocs, then you can go from there

$_SERVER['DOCUMENT_ROOT']将为您提供 htdocs 的路径,然后您可以从那里开始

EDIT

编辑

$_SERVER['REQUEST_URI']will always give you /myapp/stuff/to/seefrom your sample url above, regardless of the files location on disk or from which file it is invoked.

$_SERVER['REQUEST_URI']/myapp/stuff/to/see无论磁盘上的文件位置如何或从哪个文件调用它,都将始终从上面的示例 url 中为您提供。

so it only a matter of explodeing, array_shifting, and implodeing.

所以这只是explodeing,array_shifting和implodeing的问题。

btw, from your directory structure, it looks like you're using a framework. some frameworks have a URI/URL library that you can find useful (at least CodeIgniter has, not 100% sure with other frameworks)

顺便说一句,从您的目录结构来看,您似乎正在使用一个框架。一些框架有一个 URI/URL 库,你会发现它很有用(至少 CodeIgniter 有,其他框架不能 100% 确定)

回答by Sarfraz

Try this:

尝试这个:

 echo dirname(__FILE__);

where __FILE__is the name of current executing script, and dirnamegets folder/application folder out of it.

where__FILE__是当前执行脚本的名称,并dirname从中获取文件夹/应用程序文件夹。

More Options:

更多选择:

echo $_SERVER['DOCUMENT_ROOT']; // gives you the root dir

On PHP.net

在 PHP.net 上

回答by saurabh.in

I incurred a similar issue. Basically, in PHP if a file 'A.php' uses relative path to refer a resource, then the current path location used as a prefix to the relative path is of the top parent php under which 'A.php' is included.

我遇到了类似的问题。基本上,在 PHP 中,如果文件 'A.php' 使用相对路径来引用资源,则用作相对路径前缀的当前路径位置是包含 'A.php' 的顶级父 php。

This won't be a problem if A.php itself is top parent or when A.php sits at the same folder as is its top parent. But it will break the import otherwise.

如果 A.php 本身是顶级父级,或者 A.php 与其顶级父级位于同一文件夹中,则这不会成为问题。但否则它会破坏导入。

The best solution I found was to make the reference explicitly absolute by using DIR(which gives current location of the file) and then going to the referred by from this location for e.g. DIR."/../help.php" to refer to a file help.php sitting 1 level above A.php.

我发现的最佳解决方案是通过使用DIR(它给出文件的当前位置)使引用显式绝对化,然后从这个位置转到引用,例如DIR."/../help.php" 来引用位于 A.php 上方 1 级的文件 help.php。