包含文件而不更改其相对路径


Include a file without change its relative path

我有一个php脚本,我想与主题集成。所以我创建了一个主题文件夹,里面有一些主题,每个人都进入它的文件夹,类似于这个

css-folder
js-folder
...
themes-folder
    ----------theme1-folder
                   css-folder
                    ...
                    --------index.php 
    ----------theme2-folder
                    css-folder 
                    ... 
                    --------index.php  
...
index.php

假设我已经为我的剧本选择了主题1,如何从theme1文件夹中包含index.php进入根index.php而不更改其相对路径?

如果我使用这个

include "theme1-folder/index.php";

到根index.php然后所有到主题index.php的链接(如css和js)都显示在根文件夹中

抱歉我的英语不好

将主题路径保存到变量

$current_them = "theme1-folder";
include "$current_theme/index.php";

可能有更好的解决方案。但我以前是这样做的:

 //Define the document root path
 define("ROOT_PATH","/What/ever/My/DocumentRoot/is/");
 //Then from everywhere i can include files, writing the path from ROOT
 include(ROOT_PATH . "themes-folder/theme1-folder/index.php");

简单易用。

问题更改后编辑:

您所经历的不是php问题,而是html中的相对路径有问题。

你可以通过添加来解决这个问题

<base href="http://www.w3schools.com/images/">

在您的页眉中。这告诉浏览器所有相对路径都应该源自http://www.w3schools.com/images/.