PHP包含和相关文件路径
在我的公用html文件夹中,我有以下目录:PHP包含和相关文件路径,php,relative-path,include-path,server-side-includes,Php,Relative Path,Include Path,Server Side Includes,在我的公用html文件夹中,我有以下目录: - includes (in here I have header.php which is included in index.php) - images - styles - scripts index.php包括header.php,这是我引用样式表、脚本、图像等的地方 在我尝试直接从包含header.php的includes目录加载页面之前,一切正常,例如: 工作:www.mysite.com/index.php 不工作:www.mysite
- includes (in here I have header.php which is included in index.php)
- images
- styles
- scripts
index.php
包括header.php
,这是我引用样式表、脚本、图像等的地方
在我尝试直接从包含header.php的includes目录加载页面之前,一切正常,例如:
工作:www.mysite.com/index.php
不工作:www.mysite.com/includes/example_page.php
(其中example page还包括header.php)
在example.ph
p中,没有加载任何样式表、图像或脚本。这是因为它正在公共_html/includes中查找它们。显然,将文件路径更改为。/styles/stylesheet.css
是可行的。但这不是一个理想的解决方案,尤其是如果我必须更改header.php中每个图像的文件路径
如何检测页面是否在包含子目录中,并将文件路径设置为指向public_html
chdir();似乎不起作用。在HTML/CSS中使用绝对路径,例如
<link rel="stylesheet" href="/css/styles.css" />
^--- start at top of site document root
<img src="/images/kittens.jpg" />
^--- start at top of site document root
href/src中的所有路径都必须重写为/oldsite/images/kittens.jpg
或/oldsite/css/styles.css
http://example.com/oldsite/index.php