PHP中相对路径还是绝对路径以及如何设置
我正在开发一个网站,我正在尝试访问该网站的主目录,但我不知道如何设置。我想做的是通过 /_inc/config.php
包含 _inc/config.php
而不必使用 ../_inc/config.php
有什么想法如何添加这个吗?
I have a site that I am working on and I am trying to go to the main directory of the site and I do not know how to set that up. What I am trying to do is include _inc/config.php
through /_inc/config.php
instead of having to use ../_inc/config.php
Any ideas how to add this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,处理路径时需要解决两个不同的问题:
include
文件)这是两种不同的野兽。
文件系统路径
如果您的应用程序有单个入口点(例如,在检查 URL 查询字符串后委托给适当的业务逻辑的
index.php
),那么您可以在该文件内执行以下操作,然后您可以通过附加到
$ROOT_DIR
来为发行版中的任何文件组成文件系统路径。如果您的应用程序没有单一入口点,则此选项不可用,您只剩下以下替代方案:
从当前入口脚本中调用“路径解析”函数,将脚本到基目录的相对路径作为参数传递,例如:
// 对于 $ROOT_DIR.'/commands' 中的脚本,执行以下操作:
$ROOT_DIR =resolve_root(__FILE__, DIRECTORY_SEPARATOR.'commands');
附加的相对路径信息将使
resolve_root($filename, $relative)
能够确定dirname($filename)
的哪一部分是应用程序根目录。但是,该技术很笨重,并且要求您在每个应用程序入口点内对相对路径名进行硬编码,因此我建议不要使用它。要求您的应用程序配置包含具有“根”路径的硬编码变量,类似于上面的
$ROOT_DIR
。如果您的应用程序有安装程序,那么在安装过程中,您可以通过执行以下操作来让它准确解析根路径:$installer_dir = 目录名(__FILE__); // 假设这是根路径+“/install”
$ROOT_DIR = realpath($installer_dir.DIRECTORY_SEPARATOR.'..');
然后安装程序会将此路径写入您的应用程序配置。如果您的应用程序没有安装程序,那么您需要手动将基本路径硬编码到您的配置或使用其他选项。
使用
$_SERVER['DOCUMENT_ROOT']
并不是解决此问题的理想方法。 仅当您的应用程序安装在文档根目录时,它才会执行此任务。如果您的文档根目录是/var/www
并且您的应用程序安装在/var/www/app
中,那么您无法知道应该添加/app
到路径。更新:
关于提到在 Linux 上运行
pwd
的答案:请不要这样做。曾经。为每个 HTTP 请求调用系统 shell 会比用户单击链接的速度更快地终止 Web 服务器。它可以适用于已知用户数量较少且对性能没有真正期望的内部应用程序,但如果您愿意使用快速而肮脏的解决方案,为什么不简单地硬编码该路径呢?URL 路径
在任何实际应用程序中,您还需要创建与应用程序的基本 URL 相关的 URL(例如,执行
)。
同样,如果您的应用程序有一个入口点(例如
index.php
),那么您可以使用$_SERVER['REQUEST_URI']
来解析您的应用程序的“基本”URL应用。一个简单的实现(应该适合您)很简单:它是index.php
之前的 URL 路径的一部分。但是,可以解决不同 Web 服务器和 PHP 配置的怪癖的防弹实现是 稍微复杂一些。如果您的应用程序没有单个入口点,那么您会遇到与文件系统路径相同的问题。解决方案也是相同的:手动对配置中的 URL 进行硬编码,让安装程序在安装过程中对 URL 进行硬编码,或者根据需要使用相对路径的硬编码知识来解析它。
Generally speaking, there are two different problems to solve when dealing with paths:
include
a file)These are two different beasts.
Filesystem paths
If your application has a single point of entry (e.g. an
index.php
which then delegates to the appropriate business logic after inspecting the URL query string), then you can doinside that file, and then you can compose filesystem paths for any file in your distribution by appending to
$ROOT_DIR
.If your application does not have a single point of entry, then this option is not available and you are left with these alternatives:
Call a "path-resolution" function from your current entry script, passing the relative path of the script to the base directory as a parameter, for example:
// for a script in $ROOT_DIR.'/commands' do this:
$ROOT_DIR = resolve_root(__FILE__, DIRECTORY_SEPARATOR.'commands');
The additional relative path information will enable
resolve_root($filename, $relative)
to figure out which part ofdirname($filename)
is the application root. However, the technique is unwieldy and requires that you hardcode relative path names inside every application entry point, so I would recommend against ever using it.Require that your application's configuration includes a hardcoded variable with the "root" path, similar to
$ROOT_DIR
above. If your application has an installer, then during installation you can have it resolve the root path accurately by doing something like:$installer_dir = dirname(__FILE__); // assume this is root path + "/install"
$ROOT_DIR = realpath($installer_dir.DIRECTORY_SEPARATOR.'..');
The installer would then write this path to your application configuration. If your application has no installer, then you need to either hardcode the base path to your configuration manually or use another option.
Using
$_SERVER['DOCUMENT_ROOT']
is not a satisfactory solution to this problem. It does the job only if your application is installed at the document root. If your document root is/var/www
and your application is installed in/var/www/app
, then you have no way of knowing that you should add/app
to the paths.Update:
Regarding the answer that mentions running
pwd
on Linux: please don't do that. Ever. Invoking the system shell for each HTTP request is going to kill your web server faster than your users can click on your links. It could work for an internal application that has a known small number of users and no real expectations of performance, but why not simply hardcode that path if your are fine with a quick and dirty solution?URL paths
Inside any real application you will also need to create URLs relative to the base URL of your application (for example, to do
<img src="$BASE_URL/images/icon.png" />
).Again, if your application has a single entry point (e.g.
index.php
) then you can use$_SERVER['REQUEST_URI']
to resolve the "base" URL of your application. A simple implementation (that should work fine for you) is simple: it's the part of the URL path that comes beforeindex.php
. However, a bulletproof implementation that works around the quirks of different web servers and PHP configurations is somewhat more complicated.If your application does not have a single entry point, then you have the same problem as with filesystem paths. The solution is also the same: either hardcode the URL in your configuration manually, have an installer hardcode it during installation, or resolve it using hardcoded knowledge of your relative paths as required.
包括通过文件系统或 http 进行的工作。
如果您使用 include /some/file.php PHP 会在服务器根目录中查找。
您可以使用
或更改 php.ini(或.htaccess)中的 open_basedir。
IE
open_basedir=/some/where/ 使用该根目录中的 /file.php 而不是服务器的根目录或文档根目录。另请检查 php.ini 中的“路径和目录”部分
includes work via either filesystem or http.
if you use include /some/file.php PHP looks in the server root.
You can use
or perhaps change open_basedir in php.ini (or.htaccess).
i.e.
open_basedir=/some/where/ to use /file.php from THAT root directory instead of the server's root or document root. Also check the "paths and directories" section in php.ini
我使用绝对路径,但我也使用引导文件启动任何自定义 PHP 项目,在该文件中,我根据从
$_SERVER
提取的值将最常用的路径定义为常量。这就是我定义根路径的方式:
路径
LOCAL_PATH_ROOT
是文档根目录。通过 HTTP 访问同一路径时,路径HTTP_PATH_ROOT
是等效的。此时,可以使用以下代码将任何本地路径转换为 HTTP 路径:
如果您想确保与基于 Windows 的服务器的兼容性,您还需要将目录分隔符替换为 URL 分隔符:
这是完整的我用于 PHP PowerTools 样板 的引导代码:
如果将上述代码添加到您自己的项目中,此时输出所有用户常量(可以使用 get_define_constants(true) 来完成)应该给出如下所示的结果:
I use absolute paths, but I also start any custom PHP project with a bootstrap file where I define the most commonly used paths as constants, based on values extracted from
$_SERVER
.This is how I define my root paths :
The path
LOCAL_PATH_ROOT
is the document root. The pathHTTP_PATH_ROOT
is the equivalent when accessing the same path via HTTP.At that point, converting any local path to an HTTP path can be done with the following code :
If you want to ensure compatibility with Windows based servers, you'll need to replace the directory seperator with a URL seperator as well :
Here's the full bootstrap code that I'm using for the PHP PowerTools boilerplate :
If you add the above code to your own project, outputting all user constants at this point (which can do with
get_defined_constants(true)
should give a result that looks somewhat like this :