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

查看:95
本文介绍了如何格式化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天全站免登陆