【问题标题】:PHP pathing troublePHP路径问题
【发布时间】:2013-02-26 18:20:51
【问题描述】:

我有一个文件,functions.php。 该文件中有一些指向特定位置的函数,例如:

function returnFolders() {
    $folders = array_filter(glob("images/*"), 'is_dir');
    return $folders;
}

此函数返回 images/ 文件夹中的目录,其中 functions.php 文件所在的位置。

问题是,如果我从另一个文件夹中的脚本中包含这个 function.php,而不是 function.php 所在的文件夹,这个函数将不会给出正确的结果。

怎么解决,所以总是返回functions.php所在的文件夹?

路径如下所示:

common/functions.php
common/images/1
common/images/2
common/crons/im_including_functions.php
dir1/im_also_including_functions.php
d3/me_too.php

【问题讨论】:

    标签: php path path-finding


    【解决方案1】:

    查看magic constants

    例如,您可以使用__DIR__ 获取当前脚本目录。

    在不了解您的目录结构的情况下,我真的无法更具体。


    再想一想,在我看来,对于您的情况,更好的解决方案是使用images 文件夹的绝对路径。这样,无论这个函数在哪里运行,它都会在同一个地方寻找images

    【讨论】:

    • 结构如下所示: common/functions.php common/images/1 common/images/2 common/crons/im_include_functions.php dir1/im_also_including_functions.php dir2/me_too_including_functions.php 那么这会起作用吗?
    【解决方案2】:

    关于相对路径和绝对路径 当您使用相对路径时,它从运行您的代码的文件开始

    所以首先你应该了解这两种路径

    http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm

    稍后您可以找到解决此问题的最佳方法

    关于路径的php函数的一点帮助 http://php.net/manual/en/function.realpath.php

    希望有帮助 ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2014-12-30
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多