php PHP如何找到应用程序根?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3952590/
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
PHP how to find application root?
提问by
I'm having problems with my include files. I don't seem to be able to figure out how to construct my URLs when I use require_once('somefile.php'). If I try to use an include file in more than one place where the directory structures are different, I get an error that the include file cannot be found.
我的包含文件有问题。当我使用 require_once('somefile.php') 时,我似乎无法弄清楚如何构建我的 URL。如果我尝试在多个目录结构不同的地方使用包含文件,则会收到找不到包含文件的错误消息。
In asp.net, to get my application root path, I can use ~/directory/file.aspx. The tild forward slash always knows that I am referencing from my website root and find the file no matter where the request comes from within my website. It always refers back to the root and looks for the file from there.
在asp.net 中,要获取我的应用程序根路径,我可以使用~/directory/file.aspx。波浪号正斜杠总是知道我是从我的网站根目录引用的,无论请求来自我网站内的哪个位置,都可以找到该文件。它总是引用回根并从那里查找文件。
QUESTION:How can I get the root path of my site? How can I do this so I can reuse my include files from anywhere within my site? Do I have to use absolute paths in my URLs?
问题:如何获取我网站的根路径?我怎样才能做到这一点,以便我可以从我网站的任何地方重用我的包含文件?我必须在我的 URL 中使用绝对路径吗?
Thank you!
谢谢!
采纳答案by Jonathan Kuhn
There is $_SERVER['DOCUMENT_ROOT']
that should have the root path to your web server.
有$_SERVER['DOCUMENT_ROOT']
应该有根路径到Web服务器。
Edit: If you look at most major php programs. When using the installer, you usually enter in the full path to the the application folder. The installer will just put that in a config file that is included in the entire application. One option is to use an auto prepend fileto set the variable. another option is to just include_once()the config file on every page you need it. Last option I would suggest is to write you application using bootstrappingwhich is where you funnel all requests through one file (usually with url_rewrite). This allows you to easily set/include config variables in one spot and have them be available throughout all the scripts.
编辑:如果您查看大多数主要的 php 程序。使用安装程序时,您通常会输入应用程序文件夹的完整路径。安装程序会将其放入包含在整个应用程序中的配置文件中。一种选择是使用自动前置文件来设置变量。另一种选择是在您需要的每个页面上只包含_once()配置文件。我建议的最后一个选项是使用引导程序编写应用程序,这是您通过一个文件(通常使用 url_rewrite)汇集所有请求的地方。这使您可以轻松地在一个位置设置/包含配置变量,并使它们在所有脚本中都可用。
回答by barbushin
I usually store config.php
file in ROOT directory, and in config.php
I write:
我通常将config.php
文件存储在ROOT 目录中,并在config.php
我写:
define('ROOT_DIR', __DIR__);
define('ROOT_DIR', __DIR__);
And then just use ROOT_DIRconstant in all other scripts.
Using $_SERVER['DOCUMENT_ROOT']
is not very good because:
然后在所有其他脚本中使用ROOT_DIR常量。使用$_SERVER['DOCUMENT_ROOT']
不是很好,因为:
- It's not always matching ROOT_DIR
- This variable is not available in CGImode (e.x. if you run your scripts by CRON)
- 它并不总是匹配ROOT_DIR
- 此变量在CGI模式下不可用(例如,如果您通过 CRON 运行脚本)
回答by Geoff Kendall
It's nice to be able to use the same code at the top of every script and know that your page will load properly, even if you are in a subdirectory. I use this, which relies on you knowing what your root directory is called (typically, 'htdocs' or 'public_html':
很高兴能够在每个脚本的顶部使用相同的代码,并且知道您的页面将正确加载,即使您位于子目录中。我使用这个,这取决于你知道你的根目录叫什么(通常是“htdocs”或“public_html”:
defined('SITEROOT') or define('SITEROOT', substr($_SERVER['DOCUMENT_ROOT'], 0, strrpos($_SERVER['DOCUMENT_ROOT'], 'public_html')) . 'public_html');
With SITEROOT defined consistently, you can then access a config file and/or page components without adapting paths on a script-by-script basis e.g. to a config file stored outside your root folder:
通过一致定义的 SITEROOT,您可以访问配置文件和/或页面组件,而无需逐个脚本地调整路径,例如存储在根文件夹之外的配置文件:
require_once SITEROOT . "/../config.php";
回答by Petah
You should use the built in magic constants to find files. __FILE__
and __DIR__
. If you are on PHP < 5.3 you should use dirname(__FILE__)
您应该使用内置的魔法常量来查找文件。__FILE__
和__DIR__
。如果您使用的是 PHP < 5.3,则应该使用dirname(__FILE__)
E.g.
例如
require_once __DIR__.'/../../include_me.php';
$_SERVER['DOCUMENT_ROOT']
is not always guaranteed to return what you would expect.
$_SERVER['DOCUMENT_ROOT']
并不总是保证返回您所期望的。
回答by Simon
Define it in a config file somewhere.
在某处的配置文件中定义它。
Assuming you're using an MVC style where everything gets routed through a single index.php then
假设您使用的是 MVC 样式,其中所有内容都通过单个 index.php 进行路由,然后
realpath('.');
Will show you the path to the current working directory (i.e where index.php is)
将显示当前工作目录的路径(即 index.php 所在的位置)
So then you can define this as
那么你可以将其定义为
define('PROJECT_ROOT', realpath('.'));
If it's not MVC and you need it to work for files in subfolders then you can just hard code it in a config file
如果它不是 MVC 并且您需要它来处理子文件夹中的文件,那么您可以在配置文件中对其进行硬编码
define('PROJECT_ROOT', 'C:/wamp/www/mysite');
Then when including something you can do;
然后当包括一些你可以做的事情时;
include PROJECT_ROOT . '/path/to/include.php';
回答by Petah
You could alternativly set the base directory in your .htaccess
file
您可以在.htaccess
文件中设置基本目录
SetEnv BASE_PATH C:/wamp/www/mysite/
Then in PHP you can reference it with $_SERVER['BASE_PATH']
然后在 PHP 中你可以引用它 $_SERVER['BASE_PATH']
回答by sushil bharwani
Try this:
尝试这个:
$_SERVER['DOCUMENT_ROOT']