【问题标题】:Include a file from the root从根目录包含文件
【发布时间】:2016-08-18 03:54:36
【问题描述】:

这是我的应用程序的结构:

core
    application
        controller_base.class.php
        registry.class.php
        router.class.php
        template.class.php
    controller
    include
        config.php
    model
    views
index.php

文件 config.php 包含在 index.php 文件中(这没有问题),这是它包含的内容:

<?php

/*** include the controller class ***/
include(ROOT . '/core/application/controller_base.class.php');

/*** include the registry class ***/
include(ROOT . '/core/application/registry.class.php');

/*** include the router class ***/
include(ROOT . '/core/application/router.class.php');

/*** include the template class ***/
include(ROOT . '/core/application/template.class.php');


/*** autoload model classes ***/
function __autoload($class_name) {
    $filename = strtolower($class_name) . '.class.php';
    $file = ROOT . '/core/model/' . $filename;

    if (!file_exists($file)) {
        return false;
    }

    // include the $class_name class
    include($file);
}

$registry = new registry;

你必须知道常量 ROOT 是这样定义的:

define('ROOT', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));

此常量包含值/Lab/Livre/POO/TP1,当然它是应用程序的根。

但是我在 config.php 中包含的所有文件都存在问题。 PHP 向我抛出一个错误,指出在 /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php 中找不到文件。

所以问题很清楚。我希望 PHP 从应用程序的根目录(index.php 所在的位置)查找文件,但它从 config.php 所在的文件夹中查找。

我有这个问题很长时间了,但我想一劳永逸地找到解决方案。

我不能让它做我想做的事!我希望有人能帮助我。
非常感谢。

P.S.:换句话说,我想使用绝对路径来包含文件。

P.S.:这是完整的错误文本:

Warning: include(/Lab/Livre/POO/TP1/core/application/controller_base.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4

Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/controller_base.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4

Warning: include(/Lab/Livre/POO/TP1/core/application/registry.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7

Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/registry.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7

Warning: include(/Lab/Livre/POO/TP1/core/application/router.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10

Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/router.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10

Warning: include(/Lab/Livre/POO/TP1/core/application/template.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13

Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/template.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13

【问题讨论】:

  • 你能把完整的错误文本复制给我们吗?
  • 完成!对不起,我忘了。
  • 因此 php 不会尝试将所需的文件加载到与 config.php 相同的文件夹中。它没有找到/Lab/Livre/POO/TP1/core/application/ 中的文件。
  • 你试过dirname(__FILE__) . ROOT . '/core/application/template.class.php'
  • 感谢您的回答!明天我会尝试所有这些,因为它是法国的午夜。

标签: php file model-view-controller include


【解决方案1】:

我找到了解决问题的方法。我将 ROOT 常量设置为此:

define('ROOT', $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));

但是出了什么问题?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2019-07-12
    • 2016-11-24
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多