PHP 获取“应用程序根目录”在 Windows 上的 Xampp 中正确

发布于 2024-08-31 19:36:58 字数 1237 浏览 3 评论 0原文

我在 StackOverflow 上找到了这个帖子 关于如何从我的网络应用程序内部获取“应用程序根目录”。

然而,该线程中建议的大多数方法很难应用于 Windows 上的 Xampp。比方说,我有一个“common.php”,它位于我的网络应用程序的应用程序目录中:

/
/app/common.php
/protected/index.php

在我的 common.php 中,我得到的是这样的:

define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
define('ABSPATH', dirname(__FILE__));
define('COMMONSCRIPT', $_SERVER['PHP_SELF']);

在我需要 /protected/index 中的 common.php 之后。 php,我发现这个:

C:/xampp/htdocs     //<== echo DOCROOT;
C:\xampp\htdocs\comic\app //<== echo ABSPATH
/comic/protected/index.php //<== echo COMMONSCRIPT

所以最麻烦的部分是路径分隔符不通用,加上,似乎所有 $_SERVER[] asso 数组中的超全局变量(例如 $_SERVER['PHP_SELF'])与“调用者”脚本相关,而不是“被调用者”脚本。

看来我只能依靠 dirname(__FILE__) 来确保它始终返回 common.php 文件的绝对路径。

我当然可以解析 DOCROOT 和 ABSPATH 的返回值,然后计算正确的“应用程序根”。例如,我可以比较 htdocs 之后的部分,并用斜杠替换所有反斜杠以获得类似 UNIX 的路径,

我想知道这是处理这个问题的正确方法吗?如果我在 LAMP 环境中部署 Web 应用程序会怎样?这种依赖环境的方法会把我炸死吗?

我使用过一些 PHP 框架,例如 CakePHP 和 CodeIgniter,坦率地说,它们只能在 LAMP 或 WAMP 上工作,但我不确定他们是如何实现如此优雅的解决方案的。

非常感谢您提供的所有提示和建议。

I found this thread on StackOverflow about how to get the "Application Root" from inside my web app.

However, most of the approaches suggested in that thread can hardly be applied to my Xampp on Windows. Say, I've got a "common.php" which stays inside my web app's app directory:

/
/app/common.php
/protected/index.php

In my common.php, what I've got is like this:

define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
define('ABSPATH', dirname(__FILE__));
define('COMMONSCRIPT', $_SERVER['PHP_SELF']);

After I required the common.php inside the /protected/index.php, I found this:

C:/xampp/htdocs     //<== echo DOCROOT;
C:\xampp\htdocs\comic\app //<== echo ABSPATH
/comic/protected/index.php //<== echo COMMONSCRIPT

So the most troublesome part is the path delimiters are not universal, plus, it seems all
superglobals from the $_SERVER[] asso array, such as $_SERVER['PHP_SELF'], are relative to the "caller" script, not the "callee" script.

It seems that I can only rely on dirname(__FILE__) to make sure this always returns an absolute path to the common.php file.

I can certainly parse the returning values from DOCROOT and ABSPATH and then calculate the correct "application root". For instance, I can compare the parts after htdocs and substitute all backslashes with slashes to get a unix-like path

I wonder is this the right way to handle this? What if I deploy my web app on a LAMP environment? would this environment-dependent approach bomb me out?

I have used some PHP frameworks such as CakePHP and CodeIgniter, to be frank, They just work on either LAMP or WAMP, but I am not sure how they approached such a elegant solution.

Many thanks in advance for all the hints and suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-09-07 19:36:58

dirname(__FILE__) 是获取路径的常用方法。实际上,如果您的脚本仅在 PHP 5.3 或更高版本上运行,则 __DIR__ 会执行相同的操作,并且键入的时间会短一些。

我认为您涉及 common.php 的策略有点多余。对于初学者来说,DOCROOTCOMMONSCRIPT 毫无用处 - 它们只是复制一些已经是全局的数据。 (所有 PHP 开发人员都知道 $_SERVER['PHP_SELF'] 是什么,但我个人如果不查的话就不知道 COMMONSCRIPT 是什么意思。)

其次,它可能更多麻烦大于其价值。比较从 /protected/protected.php 获取 /protected/protected2.php 路径的一些不同方法:

ABSPATH . "/../protected/protected2.php";
"protected2.php"
dirname(__FILE__) . "/protected2.php";

所有这些方法都是有效的,但有些方法比其他方法更容易。这只是说明您所要求的(获取应用程序根)可能并不总是您所需要的。所以我想说,没有什么灵丹妙药可以解决你所有的问题。如果您发现 ABSPATH 常量可以帮助您,请使用它。

你的问题的第二部分:正斜杠可以在 Windows 上使用,甚至可以与反斜杠混合使用。例如:

include("c:\whatever\whatever/something/anything.php");

可以在 Windows 上的 PHP 中运行(至少在任何 5.0+ 版本上;不确定 4.x 是否如此)。反斜杠在 Unix 上不起作用,因此只需始终键入正斜杠即可实现跨平台安全。

如果您正在编写跨平台脚本,您永远不想在驱动器号中进行编码。相反,应该这样写:

include("/whatever/whatever/something/anything.php");

最初的 / 将被 Windows 上的 PHP 解释为 c:\。 (我不知道如何以跨平台的方式指向不同的驱动器号。)

HTH

dirname(__FILE__) is a common method of getting the path. Actually, if your script will only run on PHP 5.3 or later, __DIR__ will do the same thing and is a little shorter to type.

I think your strategy involving common.php is a little redundant. For starters, DOCROOT and COMMONSCRIPT are useless - they just duplicate some data that is already global. (All PHP developers know what $_SERVER['PHP_SELF'] is, but I personally would not know what COMMONSCRIPT means without looking it up.)

Second, it may be more trouble than it's worth. Compare some different ways of getting the path to /protected/protected2.php from /protected/protected.php:

ABSPATH . "/../protected/protected2.php";
"protected2.php"
dirname(__FILE__) . "/protected2.php";

All of these are valid, but some are easier than others. This just illustrates that what you are asking for (getting the application root) may not always be what you need. So I would say, there is no magic path bullet that will solve all of your problems. If you find yourself in a situation in which the ABSPATH constant helps you, use it.

Second part of your question: Forward slashes will work on Windows, even mixed with backslashes. For example:

include("c:\whatever\whatever/something/anything.php");

will work in PHP on Windows (at least on any 5.0+ version; not sure about 4.x). Backslashes will not work on Unix, so just always type forward slashes and you'll be cross-platform-safe.

If you are writing a cross-platform script, you never want to code in a drive letter. Instead, write it like this:

include("/whatever/whatever/something/anything.php");

The initial / will be interpreted by PHP on Windows as c:\. (I don't know how to point to a different drive letter in a cross-platform way.)

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文