【问题标题】:Config.php File Not Working Properly with dirname(__FILE__)Config.php 文件无法正常使用 dirname(__FILE__)
【发布时间】:2013-04-10 19:42:37
【问题描述】:

我的配置文件如下所示:

<?php
define('ROOT_DIR', dirname(_FILE_));
?>

然后是我的html:

<?php 
require_once ('../../../../config.php');  // back to app root
?>

<?php
$pageTitle = 'title';
require_once(ROOT_DIR.'includes/content.php');
?>

我知道我的配置文件正在加载,因为我可以在查看源代码时看到我的配置 cmets。

但是,否则我只会得到一个空白页。有人告诉我我做错了什么吗?

编辑:

错误报告产量

Warning: require_once(./includes/content.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/html/BLX2012/prototypes/19/demo.16/events/2013/PIC/LasVegas/index.php on line 10

Fatal error: require_once() [function.require]: Failed opening required './includes/content.php' (include_path='.:/Applications/MAMP/bin/php/php5.3.6/lib/php') in /Applications/MAMP/html/BLX2012/prototypes/19/demo.16/events/2013/PIC/LasVegas/index.php on line 10

[对不起,我希望以上内容更具可读性]

【问题讨论】:

    标签: php config dirname


    【解决方案1】:

    _FILE_ 应该是 __FILE__(每边 2 个下划线)。

    所以你的ROOT_DIR 常量设置错误,PHP 不知道去哪里找。

    【讨论】:

    • 哇,好的,我想念的东西。作为一个新手,不知道两边都有 [2]。已修复,谢谢! thnx 伙计们给了我一些爱! :]
    • @conceive 没有问题,很高兴为您服务 :) 有很多次我犯过这种错误,即使在专业编码 PHP 7 年后!
    【解决方案2】:

    您在require_once(ROOT_DIR.'includes/content.php'); 中缺少/

    将其更改为: require_once(ROOT_DIR.'/includes/content.php');

    【讨论】:

    • 真的吗?我刚刚尝试过 - 仍然无法正常工作。在使用include() 之前,我从未使用过前导/。我错过了什么?
    • @Populus- 实际上,我在文件引用中需要前导 / 也是正确的。我不确定为什么会出现这种情况,因为我从来没有将它们用于我的常规include() 电话。谢谢你的帮助!! :]
    【解决方案3】:

    在脚本的开头,添加此语句以查看是否有任何错误:

    ini_set('error_reporting', E_ALL);
    

    content.php 是否有可能在错误的路径中包含文件?

    【讨论】:

    • @Simon- 不,路径似乎正确。是的,我用过ini_set('display_errors',1); error_reporting(E_ALL);
    • 它清楚地说文件(content.php)不存在是正确的路径吗?
    • 是的,实际上你是对的。我只是不明白我错过了什么。因为理论上它在正确的位置。得再检查一下,谢谢...
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2011-09-24
    相关资源
    最近更新 更多