【问题标题】:relative path in require_once doesn't workrequire_once 中的相对路径不起作用
【发布时间】:2011-07-19 07:32:11
【问题描述】:

我有以下结构

otsg
 > class
   > authentication.php
   > database.php
   > user.php
 > include
   > config.inc.php
   > encryption.php
   > include.php
   > session.php
 > index.php
 > registration.php

include.php 文件有以下内容

ini_set('display_errors', 1);
error_reporting(E_ALL);

ini_set('include_path',ini_get('include_path').':/Applications/MAMP/htdocs/otsg/:');
require_once 'config.inc.php';
require_once '../class/database.php';
require_once '../class/user.php';
require_once 'encryption.php';
require_once 'session.php';
require_once '../class/authentication.php';

在我包含的 index.php 页面中

require_once 'include/include.php';

当我打开页面 index.php 时,我收到以下警告和致命错误。我不明白是什么导致了这个错误。当我给出绝对路径时,它可以工作。但我相信绝对路径不是一个好主意。

Warning: require_once(../class/database.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/otsg/include/include.php on line 9

Fatal error: require_once() [function.require]: Failed opening required '../class/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/htdocs/otsg/include/:') in /Applications/MAMP/htdocs/otsg/include/include.php on line 9

提前致谢

【问题讨论】:

  • 我通常在做 Objective-C 并尝试着手编写 php 脚本,很遗憾地看到我已经浪费了 2 个小时来弄清楚如何在 PHP 中正确包含文件。使用 X-code 让事情变得如此简单。
  • 经常会遇到此错误,要快速排除故障,请按照以下步骤操作:stackoverflow.com/a/36577021/2873507

标签: php warnings fatal-error require-once


【解决方案1】:

对于 php 版本 5.2.17 __DIR__ 将不起作用,它只适用于 php 5.3

但是对于旧版本的php dirname(__FILE__) 完美

比如这样写

require_once dirname(__FILE__) . '/db_config.php';

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的问题,一切正常,直到我在另一个包含中包含一个包含。

    require_once '../script/pdocrud.php';  //This worked fine up until I had an includes within another includes, then I got this error:
    Fatal error: require_once() [function.require]: Failed opening required '../script/pdocrud.php' (include_path='.:/opt/php52/lib/php')
    

    解决方案 1.(我的公共 html 文件夹名称的硬编码,但它有效):

    require_once $_SERVER["DOCUMENT_ROOT"] . '/orders.simplystyles.com/script/pdocrud.php';
    

    解决方案 2.(上面关于 DIR 仅在 php 5.3 之后才有效的不受欢迎的评论,但它有效):

    require_once __DIR__. '/../script/pdocrud.php';
    

    解决方案 3.(我看不到任何缺点,它在我的 php 5.3 中完美运行):

    require_once dirname(__FILE__). '/../script/pdocrud.php';
    

    【讨论】:

      【解决方案3】:

      使用

      __DIR__
      

      获取脚本的当前路径,这应该可以解决您的问题。

      所以:

      require_once(__DIR__.'/../class/user.php');
      

      这将防止您可以从不同的文件夹运行 PHP 脚本,因此相关路径将不起作用。

      编辑:斜线问题已修复

      【讨论】:

      • 当心! __DIR__ 是 PHP 5.3 中的新功能。如果您需要此脚本在早期版本的 PHP 上运行,则应使用 dirname(__FILE__) 代替。
      • 请注意,您应该在__DIR__ 之后手动包含斜杠。所以:require_once(__DIR__.'/../class/user.php'); 根据documentation,除了根目录之外,后面的斜杠被省略了。
      • 感谢您的帮助,但我仍然不明白为什么需要__DIR__。 PHP 文档似乎说 PHP 默认会搜索当前脚本的目录。谁能解释一下?
      • @David 我认为当您同时从 2 个地方导入文件时需要这样做。例如,您有一个带有require "../a.php" 的文件。如果此文件是从dir1/dir2/x.php 中的另一个文件(x)导入的,则包含的文件将为dir1/a.php,但如果x 在dir1/dir2/dir3/x.php 中,则包含的文件将为dir1/dir2/a.php,等等。或者我错了吗?
      • 这里有一个针对此常见错误的故障排除清单:stackoverflow.com/a/36577021/2873507
      【解决方案4】:

      在我的情况下它不起作用,即使使用 __DIR__getcwd() 它总是选择错误的路径,我通过在我需要的每个文件中使用项目的绝对基本路径定义一个成本来解决:

      if(!defined('THISBASEPATH')){ define('THISBASEPATH', '/mypath/'); }
      require_once THISBASEPATH.'cache/crud.php';
      /*every other require_once you need*/
      

      我有 php 5.4.10 的 MAMP,我的文件夹层次结构是基本的:

      q.php 
      w.php 
      e.php 
      r.php 
      cache/a.php 
      cache/b.php 
      setting/a.php 
      setting/b.php
      

      ....

      【讨论】:

        猜你喜欢
        • 2013-02-19
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 2017-07-12
        • 2013-06-30
        • 2015-08-30
        • 2011-03-17
        • 2014-06-25
        相关资源
        最近更新 更多