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

时间:2020-12-20 15:50:22

If my URL is http://www.server.com/myapp/stuff/to/see and my directory structure on disk is htdocs/myapp/*, how can I extract the /myapp part? 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部分?如果没有应用程序文件夹且应用程序root只是'/',该怎么办?是否有预定义的变量来获取该值?

What I want is a function that is able to trim /myapp off 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/see is 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,我想知道如何得到我上面提到的。

6 个解决方案

#1


12  

For the web root, there is DOCUMENT_ROOT as 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根目录,有几个答案中指出的DOCUMENT_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__)定义应用程序根目录;或配置设置并将其存储在整个应用程序中可用的常量中。

#2


15  

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 local path is /something/htdocs/myapp/stuff/to/see.

当您使用http://www.server.com/myapp/stuff/to/see访问页面,并且您的Web服务器的文档根位于/ something / htdocs /时,则当前本地路径为/ something / htdocs / myapp /东东/到/看。

There is no definite solution to get /something/htdocs/myapp/ as a result now, because there is no clear definition which path 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']
  • 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/myapp/stuff/to/see/index.php $ _SERVER ['SCRIPT_FILENAME']
  • /something/htdocs/
    $_SERVER['DOCUMENT_ROOT']
  • / something / htdocs / $ _SERVER ['DOCUMENT_ROOT']
  • /myapp/stuff/to/see
    $_SERVER['REQUEST_URI']
  • / myapp / stuff / to / see $ _SERVER ['REQUEST_URI']
  • /myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_NAME'] and $_SERVER['PHP_SELF']
  • /myapp/stuff/to/see/index.php $ _SERVER ['SCRIPT_NAME']和$ _SERVER ['PHP_SELF']

#3


3  

<?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 folder or not.

然后,您可以简单地总是执行$ path = ABSPATH。 '/path/to/file.php';无论您的本地副本是否在子目录文件夹中。


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()调用,具体取决于该文件的位置。为从webapp的根目录传递的每个目录添加一个。

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 /中,并且您的“global include”位于/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)

ie。:2个额外的dirname()调用,因为webapp根目录中有两个额外的文件夹(包括/ framework)


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.php you 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_ROOT FROM ABSPATH:

然后获取URL路径,这是删除DOCUMENT_ROOT FROM ABSPATH的简单问题:

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

#4


1  

$_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/see from 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,无论磁盘上的文件位置或从哪个文件调用它。

so it only a matter of explodeing, array_shifting, and 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%肯定与其他框架)

#5


0  

Try this:

尝试这个:

 echo dirname(__FILE__);

where __FILE__ is the name of current executing script, and dirname gets folder/application folder out of it.

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

More Options:

更多的选择:

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

On PHP.net

在PHP.net上

#6


0  

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。

#1


12  

For the web root, there is DOCUMENT_ROOT as 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根目录,有几个答案中指出的DOCUMENT_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__)定义应用程序根目录;或配置设置并将其存储在整个应用程序中可用的常量中。

#2


15  

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 local path is /something/htdocs/myapp/stuff/to/see.

当您使用http://www.server.com/myapp/stuff/to/see访问页面,并且您的Web服务器的文档根位于/ something / htdocs /时,则当前本地路径为/ something / htdocs / myapp /东东/到/看。

There is no definite solution to get /something/htdocs/myapp/ as a result now, because there is no clear definition which path 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']
  • 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/myapp/stuff/to/see/index.php $ _SERVER ['SCRIPT_FILENAME']
  • /something/htdocs/
    $_SERVER['DOCUMENT_ROOT']
  • / something / htdocs / $ _SERVER ['DOCUMENT_ROOT']
  • /myapp/stuff/to/see
    $_SERVER['REQUEST_URI']
  • / myapp / stuff / to / see $ _SERVER ['REQUEST_URI']
  • /myapp/stuff/to/see/index.php
    $_SERVER['SCRIPT_NAME'] and $_SERVER['PHP_SELF']
  • /myapp/stuff/to/see/index.php $ _SERVER ['SCRIPT_NAME']和$ _SERVER ['PHP_SELF']

#3


3  

<?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 folder or not.

然后,您可以简单地总是执行$ path = ABSPATH。 '/path/to/file.php';无论您的本地副本是否在子目录文件夹中。


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()调用,具体取决于该文件的位置。为从webapp的根目录传递的每个目录添加一个。

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 /中,并且您的“global include”位于/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)

ie。:2个额外的dirname()调用,因为webapp根目录中有两个额外的文件夹(包括/ framework)


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.php you 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_ROOT FROM ABSPATH:

然后获取URL路径,这是删除DOCUMENT_ROOT FROM ABSPATH的简单问题:

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

#4


1  

$_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/see from 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,无论磁盘上的文件位置或从哪个文件调用它。

so it only a matter of explodeing, array_shifting, and 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%肯定与其他框架)

#5


0  

Try this:

尝试这个:

 echo dirname(__FILE__);

where __FILE__ is the name of current executing script, and dirname gets folder/application folder out of it.

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

More Options:

更多的选择:

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

On PHP.net

在PHP.net上

#6


0  

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。