【问题标题】:require_once is not working (relative path)require_once 不起作用(相对路径)
【发布时间】:2013-02-19 11:36:00
【问题描述】:

我的文件结构如下:

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

在 class_main.php 中,我尝试使用 require_once 来要求相同的文件,这取决于用户所在的位置(即,如果用户在管理目录中,则 require_once "../templates/foot.php" 否则 require_once "./templates/foot.php")我班级的 __destruct 函数。

我的问题是,无论我尝试什么,我总是收到相同的错误:

警告:require_once(C:\wamp\www\project\classes/templates/foot.php) [function.require-once]:无法打开流:C:\wamp\www\ 中没有这样的文件或目录project\classes\class_main.php 在第 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

【问题讨论】:

  • 如果您想要一些有用的答案,请使用require_once 发布实际代码。

标签: php require-once


【解决方案1】:
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");

【讨论】:

  • 我试过了 - require_once "..\\templates\\foot.php";require_once "..\templates\foot.php"; 都不起作用。
  • C:\wamp\www\project\classes\templates\foot.php 这个文件是否存在于那个位置?
  • 不要乱用\ /,而是使用DIRECTORY_SEPARATOR。这对于您使用的系统总是正确的。
  • 不,该文件存在于C:\wamp\www\project\templates\foot.php - 无论我如何需要它,该文件永远不会被包含在内,我总是收到类似于我上面发布的错误,或者require_once(./templates/foot.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in C:\wamp\www\project\classes\class_main.php on line 22
  • 好的,你能把包含这个文件的代码贴出来吗?另请查看下面 Edwin 的答案,您的路径正在其他地方寻找文件是正确的。但是您必须发布一些代码才能获得更快的答案
【解决方案2】:

您的 templates 文件夹位于 classes 文件夹之外。但错误显示,它在类文件夹中搜索。

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

正确地包含它。

【讨论】:

  • 怎么样?我在开始时错过了一个时期吗?我已经检查过了,我确定我正确地包含了它。
猜你喜欢
  • 2011-07-19
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 2015-08-30
  • 2011-03-17
  • 2014-06-25
  • 2018-09-15
相关资源
最近更新 更多