PHP代码没有正确链接丢失代码

时间:2013-07-06 04:46:20

标签: php

这就是我所拥有的。我有一个索引php页面,位于我的/ root /文件夹中,代码如下:

index.php

require_once("access/$template/head.php");

我的函数页面具有$ template代码的以下配置:

funcs.php

function getTemplateFiles() { 
    $directory = "models/site-templates/"; 
    $languages = glob($directory . "*");  
    return $languages; 
}

我的头部php页面包含以下代码:

head.php

<link rel='stylesheet' href='$template/css/style.css'>

我的问题是我可以将哪些代码添加到我的头页中,这个代码页将与我的头页进行通信,这是我的$模板之前的代码,其中包含我的php页面的文件位置。

示例1

$include_dir/$template/css/style.css'>

我的问题是如何包含索引页面的路径,以便我的头页像html一样读取?

默认,不起作用$template/css/style.css

仅对索引页面access/$template/css/style.css

这样需要

2 个答案:

答案 0 :(得分:0)

替换您的代码

<link rel='stylesheet' href='$template/css/style.css'>

使用此

<link rel='stylesheet' href='<?php echo $template; ?>/css/style.css'>

答案 1 :(得分:0)

我不太确定我是否理解你的问题,但这两种方法可能有所帮助:

$currentDir = dirname(__FILE__);

$currentDir将是您服务器上当前所在文件的路径。因此,如果您有这样的结构:

/access/functions/myFunction.php

并且您从myFunction.php调用此方法,$currentDir将返回'/ access / functions'。如果要包含的文件位于同一目录路径中,则可以包含如下:

$includePath = $currentDir."/myFile.php";

确定目标目录的另一种方法是使用:

$docRoot = realpath($_SERVER["DOCUMENT_ROOT"]);

$docRoot将成为您网站的首要目录(通常类似于/ usr / www / username / public_html /)

使用它,你总是知道你的顶级目录是什么,然后你可以包括这样的文件:

$includePath = $docRoot."/models/site-templates/myTemplate.css";