dsmgcse8876
2015-09-21 12:57
浏览 113
已采纳

PHP获取包含文件的文件路径

I want to include a file which includes a file.. confusing, i'll try to explain a bit more.

I'll show the folder structure first:

  • /main/
  • /main/head.php
  • /main/headertext.txt
  • /main/index.php
  • /main/chat/
  • /main/chat/headertext.txt
  • /main/chat/index.php

I include the file /main/head.php into the file /main/chat/index.php.

Now, in /main/head.php i want to include /main/chat/headertext.txt

In /main/index.php i also include /main/head.php, there i want to include /main/headertext.txt

I don't know if anyone understands me, but i want to include a text file that is in the folder of the current index.php, whatever folder i am in..

Is that possible?

图片转代码服务由CSDN问答提供 功能建议

我想要包含一个包含文件的文件..令人困惑,我会尝试解释一下。

我将首先显示文件夹结构:

  • / main /
  • / main /head.php
  • /main/headertext.txt
  • /main/index.php
  • /main/chat/
  • /main/chat/headertext.txt
  • /main/chat/index.php

    我 将文件/main/head.php包含到文件/main/chat/index.php.

    nn

    现在,在/main/head.php中我要包含/ main / chat / headertext.txt

    在/main/index.php中我还包括/main/head.php,我想要包含/main/headertext.txt

    nn

    我不知道是否有人理解我,但我想要包含当前index.php文件夹中的文本文件,无论我在哪个文件夹中。

    那可能吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanmian1085 2015-09-21 13:04
    已采纳

    You can try this:

    define('ROOT_PATH', str_replace(DIRECTORY_SEPARATOR, "/", dirname(dirname(__FILE__)))."/");
    

    And in every file you do your include with this constant. Like this:

    include(ROOT_PATH.'file.php');
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dousong8187 2015-09-21 13:04

    If I have understood correctly:

    Now, in /main/head.php i want to include /main/chat/headertext.txt

    In your /main/head.php:

    include ('../chat/headertext.txt');
    

    In /main/index.php i also include /main/head.php, there i want to include /main/headertext.txt

    In your /main/index.php:

    include ('../headertext.txt');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题