2013-03-05 127 views
2

我有一个文件结构如下:require_once不工作(相对路径)

>project 
>admin 
>classes 
    >class_main.php 
>templates 
    >foot.php 

在class_main.php,我试图用require_once要求相同的文件,这取决于用户在哪里(也就是说,如果用户在我的类的__destruct函数内部,那么该用户位于管理目录中,即require_once "../templates/foot.php",否则为require_once "./templates/foot.php")。

我的问题是,无论我怎么努力,我总是收到相同的错误:

Warning: require_once(C:\wamp\www\project\classes/templates/foot.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\project\classes\class_main.php on line 22

我曾尝试:用require_once作为一个功能

    • 使用dirname(__FILE__)__DIR__
    • 使用\\\而不是/

    我该怎么做?

    此刻的__destruct功能的完整代码:

    function __destruct() 
        { 
         if($this->admtrim($_SERVER['PHP_SELF']) == 'admin') 
         { 
          require_once "../templates/foot.php"; 
         } 
         else 
         { 
          require_once './templates/foot.php'; 
         } 
        } 
    

    要澄清,这个脚本位于C:\wamp\www\project\classes\class_main.php,我想包括文件位于C:\wamp\www\project\templates\foot.php

  • +1

    如果您想获得一些有用的答案,请使用'require_once'发布实际代码。 – 2013-03-05 07:00:05

    回答

    0
    C:\wamp\www\project\classes/templates/foot.php 
    

    这应该是自我解释,看在一个链接的前后斜线。这应该是

    C:\wamp\www\project\classes\templates\foot.php 
    

    尝试在Windows格式的反斜线

    编辑:

    正如你所提到的文件位于C:\wamp\www\project\templates\foot.php那么,为什么不提供完整的路径

    require_once("C:\wamp\www\project\templates\foot.php"); 
    
    +0

    我试过了 - require_once“\\ templates \\ foot.php”;''和'require_once“.. \ templates \ foot.php”;'两者都不起作用。 – Seabody 2013-03-05 06:53:35

    +0

    'C:\ wamp \ www \ project \ classes \ templates \ foot.php'该文件是否存在于该位置? – 2013-03-05 06:55:16

    +1

    而不是搞乱'\'和'/',使用'DIRECTORY_SEPARATOR'。对于您正在使用的系统来说,这总是正确的。 – Arjan 2013-03-05 06:59:50

    0

    您的模板文件夹位于outsi de 文件夹。但错误显示,它在类文件夹内搜索。

    C:\wamp\www\project\classes/templates/foot.php 
    

    正确地包括它。

    +0

    如何?我在开始时错过了一段时间吗?我检查过了,我确定我正确地包括了它。 – Seabody 2013-03-05 07:20:39