相对路径或绝对路径以及如何在PHP中进行设置 [英] Relative path or absolute path and how to set up in PHP

查看:152
本文介绍了相对路径或绝对路径以及如何在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?

推荐答案

通常来说,处理路径时要解决两个不同的问题:

Generally speaking, there are two different problems to solve when dealing with paths:

  1. 文件系统路径(您需要使用它来include文件)
  2. URL路径(在构建应用程序内部的URL时需要使用)
  1. Filesystem paths (which you need to use to include a file)
  2. URL paths (which you need to use when building a URL internal to your application)

这是两种不同的野兽.

如果您的应用程序只有一个入口点(例如,index.php然后在检查URL查询字符串后委托给相应的业务逻辑),则可以

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 do

$ROOT_DIR = dirname(__FILE__);

在该文件中,然后可以通过附加到$ROOT_DIR来为分发中的任何文件组成文件系统路径.

inside 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:

  1. 在任何地方都使用相对路径(这是您要避免的).
  2. 从当前输入脚本中调用路径解析"功能,将该脚本的相对路径作为参数传递给基本目录,例如:

  1. Use relative paths everywhere (this is what you would like to avoid).
  2. 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');

附加的相对路径信息将使resolve_root($filename, $relative)能够确定dirname($filename)的哪一部分是应用程序根目录.但是,该技术笨拙,并且要求您在每个应用程序入口点内对相对路径名进行硬编码,因此,建议不要使用它.

The additional relative path information will enable resolve_root($filename, $relative) to figure out which part of dirname($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.

要求您的应用程序配置包括带有"root"路径的硬编码变量,类似于上面的$ROOT_DIR.如果您的应用程序具有安装程序,那么在安装过程中,您可以通过执行以下操作使其准确解析根路径:

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.'..');

$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.

使用$_SERVER['DOCUMENT_ROOT']不能令人满意地解决此问题.仅当您的应用程序安装在文档根目录时,它才会执行作业.如果您的文档根目录是/var/www,并且您的应用程序已安装在/var/www/app中,那么您将无法知道应该在路径中添加/app.

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.

更新: 关于提到在Linux上运行pwd的答案:请不要这样做.曾经.为每个HTTP请求调用系统外壳会使您的Web服务器崩溃的速度快于用户单击链接的速度.它可以用于内部应用程序,该应用程序的用户数量很少,并且对性能没有真正的期望,但是如果您可以使用快速而又肮脏的解决方案很好,那么为什么不简单地对该路径进行硬编码呢?

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的URL(例如,执行<img src="$BASE_URL/images/icon.png" />).

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

同样,如果您的应用程序只有一个入口点(例如index.php),则可以使用$_SERVER['REQUEST_URI']来解析应用程序的基本" URL.一个简单的实现(对您来说应该很好)很简单:它是index.php之前的URL路径的一部分.但是,可以解决不同Web服务器和PHP配置问题的防弹实施是

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 before index.php. However, a bulletproof implementation that works around the quirks of different web servers and PHP configurations is somewhat more complicated.

如果您的应用程序没有单个入口点,那么您将遇到与文件系统路径相同的问题.解决方案也相同:要么手动在配置中对URL进行硬编码,要么让安装程序在安装过程中对URL进行硬编码,要么根据需要使用对相对路径的硬编码知识来对其进行解析.

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.

这篇关于相对路径或绝对路径以及如何在PHP中进行设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆