PHP页面没有获得根目录

时间:2013-12-31 16:42:31

标签: php html

我的页面位于服务器上的/ restricted目录中。我在目录/包含中包含文件。这些包括文件包含样式,标题,导航,页脚等。我的问题是我似乎无法从页面中找到包含文件/限制。下面是我在/ restricted目录中的一些代码。

<!DOCTYPE HTML>
<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>PRS Network</title>

    <?php include('includes/public_head.php'); ?>

    <!-- Google Analytics Code Goes Below Here -->

    <!-- End Google Analytics Code -->

</head>

<body> <?php include_once("includes/analyticstracking.php") ?>
    <div id="outter">
    <div id="wrapper">

        <?php include('includes/public_header.php'); ?>

        <?php include('includes/main_nav.php'); ?>

        <hr noshade width="97%">

        <?php include('includes/social.php'); ?>

我尝试在“包含”前放置“/”,但仍然无法找到包含文件。当我尝试在浏览器中加载页面时,这就是我得到的:

Warning: include(includes/public_head.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 12

Warning: include() [function.include]: Failed opening 'includes/public_head.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 12

Warning: include_once(includes/analyticstracking.php) [function.include-once]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 20

Warning: include_once() [function.include]: Failed opening 'includes/analyticstracking.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 20

Warning: include(includes/public_header.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 24

Warning: include() [function.include]: Failed opening 'includes/public_header.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 24

Warning: include(includes/main_nav.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 26

Warning: include() [function.include]: Failed opening 'includes/main_nav.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in .../html/restricted/videoarchive.php on line 26

Warning: include(includes/social.php) [function.include]: failed to open stream: No such file or directory in .../html/restricted/videoarchive.php on line 30

(注意:我在浏览器中收到的错误中删除了一些文本。但看起来它总是在/ restricted目录中查找,即使我在include之前添加“/”。)

非常感谢任何帮助!

提前致谢。

3 个答案:

答案 0 :(得分:1)

如果/ restricted和/ includes都在同一级别(同一目录),那么你需要做的就是,(当你在里面/受限制时)

调用php常量 DIR ,然后将其包装在dirname( DIR )中,这样就可以获得绝对路径。

<?php  include_once dirname(__DIR__).'/includes/blah/blah/blah.php';

你真正要做的是定义一个常量:

<?php
 define('ROOT_DIR', __DIR__);

将其保存在目录ROOT中的文件(CONSTANTS.php)中。然后将它包含在所有文件中,因此无论您在哪里/目录中,您都可以随时调用ROOT_DIR来获取目录,然后添加到您想要的目录。

答案 1 :(得分:0)

假设包含和限制在同一目录级别,您需要指向一个目录才能找到它们:

而不是:

<?php include('includes/public_header.php'); ?>

你会使用:

<?php include('../includes/public_header.php'); ?>

答案 2 :(得分:0)

如果我理解正确,你有这样的文件夹:

/path/to/website/
/path/to/website/restricted/
/path/to/website/includes/

restricted文件夹中使用此内容时

include_once("includes/analyticstracking.php")

正在寻找:

/path/to/website/restricted/includes/analyticstracking.php

如果您添加/,它正在寻找:

/includes/analyticstracking.php

这些都不存在。您需要使用..文件夹符号直接遍历一个:

include_once("../includes/analyticstracking.php")

或者相反,完全符合路径:

include_once("/path/to/website/includes/analyticstracking.php")

无论哪种方式,您都需要从脚本的当前工作目录中相对引用该文件,或者绝对从文件系统的根目录引用该文件。