如何格式化 PHP include() 绝对(而不是相对)路径? [英] How do I format a PHP include() absolute (rather than relative) path?

查看:28
本文介绍了如何格式化 PHP include() 绝对(而不是相对)路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 PHP 网站的各个页面和各个嵌套目录中,我想在相对于根目录的路径中包含一个特定文件.

On various pages throughout my PHP web site and in various nested directories I want to include a specific file at a path relative to the root.

我可以在这两个页面上放置哪个命令...

What single command can I put on both of these pages...

http://www.example.com/pageone.php

http://www.example.com/somedirectory/pagetwo.php

...包含此页面:

http://www.example.com/includes/analytics.php

这不起作用:

<?php include('/includes/analytics.php'); ?>

在 Windows 上的 IIS 中托管这有关系吗?

Does it matter that this is hosted in IIS on Windows?

推荐答案

如果给 include() 或 require()(或 *_once 版本)一个绝对路径名,该文件将被包含在内.绝对路径名在 unix 上以/"开头,在 Windows 上以驱动器号和冒号开头.

If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a "/" on unix, and with a drive letter and colon on Windows.

如果给出相对路径(任何其他路径),PHP 将按顺序搜索配置值include_path"中的目录,直到找到匹配项或没有更多目录可供搜索.

If you give a relative path (any other path), PHP will search the directories in the configuration value "include_path" in order, until a match is found or there are no more directories to search.

因此,简而言之,要包含绝对文件名,请给出绝对文件名.另请参阅函数 realpath().

So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().

如果您想设置自己的包含root",请查看 这个问题(当然是我的回答:-)

If you want to set your own include "root", have a look at this question (specifically my answer of course :-)

这篇关于如何格式化 PHP include() 绝对(而不是相对)路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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