Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP包含和相关文件路径_Php_Relative Path_Include Path_Server Side Includes - Fatal编程技术网

PHP包含和相关文件路径

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

在我的公用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.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