doubo9799 2013-12-30 15:37
浏览 119
已采纳

嵌套include_once问题

Here is my directory tree:

  • /
    • index.php
  • include/
    • functions.php
    • head.php
    • connect.php
  • sub/
    • index.php

In my head.php and connect.php both have:

include_once 'include/functions.php';

My index.php in the root folder includes these two: head.php and connect.php like this:

include_once 'include/connect.php';
include_once 'include/head.php;'

However, when my index.php in sub/ includes functions.php and head.php, they would fail to also include functions.php. Here's how I included in the sub/index.php:

include_once '../include/connect.php';
include_once '../include/head.php';

If I change in the head.php and connect.php to: include_once '../include/functions.php';

The sub/index.php would include everything normally but the index.php in the root would fail to load the functions.php.

How can I fix this?

PHP version: 5.2.*

  • 写回答

4条回答 默认 最新

  • dpylt7626401 2013-12-30 16:10
    关注

    The Error


    Include statement error in head.php and connect.php

    include_once 'include/functions.php';


    The Fix


    include_once 'functions.php';

    OR

    include_once __DIR__ . 'functions.php'; //PHP 5.3 or higher

    OR

    include_once dirname(__FILE__) . 'functions.php'; //PHP 5.2 or lower


    The Reason


    head.php and connect.php are located in the same folder as functions.php

    As suggested by @Schleis, using __DIR__ (PHP 5.3+) or dirname(__FILE__); (PHP 5.2-) will allow for relative file includes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程