PHP动态获取将包含在其他文件中的特定文件的完整绝对URL路径 [英] PHP Dynamically get complete Absolute URL Path for specific file that will be included in other files

查看:24
本文介绍了PHP动态获取将包含在其他文件中的特定文件的完整绝对URL路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置文件.这不是一个独立的文件.我将使用 require() 将此文件包含在我想要使用的页面顶部.我想动态获取此配置文件的完整绝对 url 路径,无论它的位置如何,并将其作为常量存储在自身中.例如:

I have a configuration file. This is not a stand-alone file. I will be including this file at the top of the pages that I want to use in, using require(). I want to dynamically get the complete absolute url path to this configuration file, regardless of it's location and store it as a constant within itself. For example:

物理位置:(根目录)/my_folder/configuration.php

Physical Location: (root dir)/my_folder/configuration.php

需要 URL 为: www.mydomain.com/my_folder/configuration.php

Need URL as: www.mydomain.com/my_folder/configuration.php

物理位置:(根目录)/demos/my_folder/configuration.php

Physical Location: (root dir)/demos/my_folder/configuration.php

需要 URL 为: www.mydomain.com/demos/my_folder/configuration.php

Need URL as: www.mydomain.com/demos/my_folder/configuration.php

物理位置:(根目录)/demos/site1/my_folder/configuration.php

Physical Location: (root dir)/demos/site1/my_folder/configuration.php

需要 URL 为: www.mydomain.com/demos/site1/my_folder/configuration.php

Need URL as: www.mydomain.com/demos/site1/my_folder/configuration.php

物理位置:(根目录)/demos/site2/my_folder/configuration.php

Physical Location: (root dir)/demos/site2/my_folder/configuration.php

需要 URL 为: www.mydomain.com/demos/site2/my_folder/configuration.php

Need URL as: www.mydomain.com/demos/site2/my_folder/configuration.php

到目前为止简单吗?这是真正需要的东西,使它变得复杂(IMO).考虑一下:

Simple so far? Here's what really needed and makes it complicated (IMO). Consider this:

配置文件位于: www.mydomain.com/demos/site2/my_folder/configuration.php

Config file located at: www.mydomain.com/demos/site2/my_folder/configuration.php

有嵌套文件夹: www.mydomain.com/demos/site2/1/2/3/index.php

Have nested folders: www.mydomain.com/demos/site2/1/2/3/index.php

当我按照上面的 URL 访问3"子文件夹中的 index.php 文件时,我需要配置文件的路径为 www.mydomain.com/demos/site2/my_folder/configuration.php 而不是 www.mydomain.com/demos/site2/1/2/3/my_folder/configuration.php

When I access the index.php file in the "3" sub-folder by following the URL above, I need the path to configuration file as www.mydomain.com/demos/site2/my_folder/configuration.php and not as www.mydomain.com/demos/site2/1/2/3/my_folder/configuration.php

我如何才能实现上述目标?

How can I achieve the above?

推荐答案

如果你可以依赖 $_SERVER[ 'DOCUMENT_ROOT' ] 的值,那么在 configuration.php代码>:

If you can rely on the value of $_SERVER[ 'DOCUMENT_ROOT' ], then inside configuration.php:

$path = substr( __FILE__, strlen( $_SERVER[ 'DOCUMENT_ROOT' ] ) );

$url = "www.mydomain.com{$path}";

如果它适合您的用例,您可以使用 $_SERVER[ 'HTTP_HOST' ];

If it fits your use case, you can make it more dynamic using $_SERVER[ 'HTTP_HOST' ];

我在开发中大量使用了 DOCUMENT_ROOT,因为它通常是唯一可用于构建某些自引用路径的动态变量.有一个关于如何DOCUMENT_ROOT 处理不当,特别是 Apache 不允许您使用 RewriteRule 设置值,并且在使用 mod_vhost_alias 时没有将其设置为合理的值.讨论持续了 7 到 8 年,大概来自 Apache 项目的人们拒绝改变行为,直到他们最终在今年 2.4.1 中做出改变.(我之前研究过这个,但我现在忘记了确切的变化是什么,以及它们有多令人满意.)

I've used DOCUMENT_ROOT liberally in my development, as it's often the only dynamic variable available for constructing certain self-referential paths. There's a looong running Apache bug ticket (#26052) about how DOCUMENT_ROOT is poorly handled, particularly that Apache wouldn't allow you to set the value with RewriteRule and didn't set it to a sensible value when using mod_vhost_alias. The discussion goes on over a period of 7-8 years as people presumably from the Apache project resist changing the behavior, until they finally came around and made a change this year in 2.4.1. (I looked into this previously, but I forget now what the exact changes were, and how satisfying they are.)

如果您查看票证上的评论,您会看到人们通过以下评论抵制改变行为:

If you look at the comments on the ticket you'll see people resisting changes to the behavior with comments like:

不要相信 DOCUMENT_ROOT 变量.

Don't trust the DOCUMENT_ROOT variable.

DOCUMENT_ROOT 不是、从来不是、也永远不会是查找网络内容文件系统路径的可靠方法.

DOCUMENT_ROOT is not, never was, and never will be a reliable way of finding the filesystem path to web content.

所以我建议阅读该票证上的评论,看看人们在说使用它的注意事项是什么.我已经成功地使用了它,但不知道有什么更好的方法可以在 DOCUMENT_ROOT 可用并提供必要数据的相同情况下实现相同的目标.

So I suggest reading the comments on that ticket to see what people are saying the caveats of using it are. I've used it with a lot of success and don't know of a better way to achieve the same things in the same situations that DOCUMENT_ROOT is available and provides the necessary data.

这篇关于PHP动态获取将包含在其他文件中的特定文件的完整绝对URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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