【问题标题】:php include() error when included from another filephp include() 从另一个文件中包含时出错
【发布时间】:2016-12-20 10:48:44
【问题描述】:

这是我的php文件结构:

练习-php

  • index.php
  • 型号
    • model.php
  • 连接器
    • connectionUtility.php

在模型 php 中我写: 包括'../connector/connectionUtility.php'; 在 index.php 中 包括'model/model.php';


当我运行“model.php”时,没有任何错误,但是当我运行“index.php”时,浏览器显示:

require(../connector/connectionUltility.php):打开流失败:C:\wamp64\www\php-practice\model\model.php 中没有这样的文件或目录

致命错误:require():在 C:\wamp64\www\php-practice\model 中打开所需的 '../connector/connectionUltility.php' (include_path='.;C:\php\pear') 失败\model.php


我找到了我的问题的答案。如果我写: 在“model.php”中包含“connector/connectionUtility.php”,那么我只需要在“index.php”中包含“model.php”。系统仍然会理解。


感谢大家的帮助。

【问题讨论】:

  • 使用完整路径而不是相对路径通常可以解决此类问题。即:include( $_SERVER['DOCUMENT_ROOT'] . '/connector/connectionUltility.php' ); 或使用set_include_path 之前使用include
  • @ Ilaya Raja S 当你输入.. 时,它会返回一个文件夹,所以在index.php 中它会尝试从www 方向包含它

标签: php


【解决方案1】:

尝试在model.php 中使用../connector/connectionUltility.php,在index.php 中使用connector/connectionUltility.php

练习-php

  • index.php
  • 型号
    • model.php
  • 连接器
    • connectionUtility.php

../connector/connectionUltility.php.. 表示它向后移动一个文件夹,所以在 model.php 它返回到 practice-php 方向,而不是返回到 connection/connectionUtility.php

但是当您在 index.php 中使用它时,它已经在 practise-php 方向并且由于点 .. 它会回到 www 方向而不是尝试去 connector/connectionUltility.php

抱歉英语不好

【讨论】:

  • ' "@ Ilaya Raja S 当你放 .. 它会返回一个文件夹,所以在 index.php 中它会尝试从 www 方向包含它"' 有意义,谢谢
  • 谢谢,它可以工作,但如果我在 connectionUtility.php 中包含另一个文件,可能会出现问题。有没有办法解决项目树中每个文件的问题?
  • 使用绝对文件名 __DIR__ 可能会有所帮助。
  • @Gii 我不确定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 2011-10-27
  • 2011-08-02
  • 1970-01-01
相关资源
最近更新 更多