【问题标题】:Getting Relative Paths获取相对路径
【发布时间】:2012-01-13 19:53:02
【问题描述】:

这里是超级菜鸟问题...

文件来源: www.mysite.com/blah/blah/customdir/ajax/folder/file.php

我想包括一个来源:

www.mysite.com/blah/blah/customdir/include/func/functions.php

所以我试过了:

require_once( '.../include/func/functions.php' );
require_once( '../include/func/functions.php' );
require_once( '././include/func/functions.php' );

以及大量其他变体。我怎么去呢?不能为每个文件设置顶级路径(这就是函数文件的用途......)

以上每个都给我一个错误: 警告:include(././include/func/functions.php) [function.include]:无法打开流:/home/mysite/public_html/mysite.com/customdir/ajax/folder/ 中没有这样的文件或目录第 3 行的 index.php

警告:include(././include/func/functions.php) [function.include]:无法打开流:/home/mysite/public_html/mysite.com/customdir/ajax 中没有这样的文件或目录/folder/index.php 在第 3 行

警告:include() [function.include]: 未能打开 '././include/func/functions.php' 以包含 (include_path='.:/usr/lib/php:/usr/local/lib /php') 在第 3 行的 /home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php 中

【问题讨论】:

    标签: php


    【解决方案1】:

    包含文件的方法有很多种。但是,您似乎正在尝试相对路径。在这种情况下,请尝试:

    require_once '../../include/func/functions.php';
    

    我鼓励你learn more about your options for including files。但简而言之,每个../ 都会带您返回一个目录。 ./ 代表当前目录。否则,您始终可以使用绝对路径,甚至可以使用 (gulp) 混合路径。

    【讨论】:

    • 它有效。谢谢。为什么需要双周期?蹩脚...尝试了很多东西。
    • 因为它是当前脚本上方的两个目录。查看更新。
    【解决方案2】:

    你在apache下运行吗?使用 getenv("DOCUMENT_ROOT") 并从那里设置您的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2011-12-11
      • 2011-08-11
      • 2010-09-21
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多