2014-02-23 124 views
0

好吧,假设我包含名为/ext/include.php的目录中的文件(请参阅示例1)PHP - 如何保存目录的路径?

我想要“include.php”文件中的一些代码来找出路径并保存它。因此示例1的路径设置为:/ ext/

代码必须工作,以便如果通过2个目录包含文件(请参见示例2)。 它只是保存路径为:/ ext/ext/

我试过类似的东西:__FILE__,但保存了整个目录。 我只想要包含的文件和包含的文件之间的路径。

我希望你能帮助newb出来。 :-) 请尝试并保持它。因为我正在学习它。

如果你可以同时使用一个oop和一个程序性的例子,那很酷!

例1:

|index.php 
|+ext 
| include.php 

例2:

|index.php 
|+ext 
| +ext 
| include.php 

修订/解决

我不能anwser我自己的问题为巢8个小时,SOOO ..

This is my temporarly soloution: 

我相信if回答我的问题。 当我完成我的代码时,我会回来,但这是我发现的。

我知道通过使用__FILES__我会得到该代码片段存在的文件的完整目录。通过使用$_SERVER['PHP_SELF'],我将获得当前正在执行的脚本的相对于文档根目录。

通过知道我可以得到2个字符串我可以比较它们。我可以将目录的其余部分添加到$_SERVER['PHP_SELF']

所以我可以替换看起来相像的字符串。最后,我temporarely的解决方案是这样的:

// adding the rest of the directory to $_SERVER and replacing/with \ . 
    $path_incg = str_replace('/','\\','C:/xampp/htdocs'.dirname($_SERVER['PHP_SELF']).'/'); 
// taking the parent directory of the full directory to the file. 
    $path_incd = dirname(__FILE__); 
// replacing the parts that look exactly like each other 
    self::$path = str_replace($path_incg,'',$path_incd).'\\'; 
    return self::$path; 

例1:可以说,$_SERVER['PHP_SELF']又名。包括文件是在/root/including.php并通过dirname()我们可以得到/root,并通过添加双方目录的休息和更换/ with \我们可以得到:

$var1 = c:\xampp\htdocs\root\ 

,并通过使用dirname(__FILE__)我们可以得到的目录该文件中获取包括在内,可以说这是在root/inc/include.php,然后我们得到:

$var2 = c:\xampp\htdocs\root\inc\include.php 

现在我们可以得到两个文件的目录,通过使用str_replace()

$finalpath = str_replace($path_incg,'',$path_incd).'\\'; 

最终结果将是:inc\。 如果您要将包含文件放置在包含文件下方的站点地图的任何其他位置,它将找到两个文件之间的路径。

+0

看看http://ca3.php。net/pathinfo –

+0

可能'pwd()'可以帮助你,但是我认为如果你有包含include的内容的话,总是得到正确的路径几乎是不可能的。你为什么想要这个特定的路径?无论你想要做什么,都可能有更好的解决方案。 – deceze

+0

@deceze我想让'include'文件知道它在哪里,不管你把它放在目录中的什么地方。而且它必须知道它自己和文件之间的目录是什么,做包括。 – user2453885

回答

0

假设包含文件总是存在于目录结构的主文件下面,那么下面的内容应该可以工作。

<?php 
// Include a FilePathDiff utility. This utility can be donwloaded at 
// https://gist.github.com/joelandsman/9174942 
require_once("libs/filepathdiff.class.php"); 

// Instantiate an instance of our FilePathDiff utility and set the base path. 
// We will reference this object in our include files to register their 
// relative paths. 
$fpd = new FilePathDiff(); 
$fpd->setBasePath(__FILE__); 

// Include some files... 
require_once('ext/index.php'); 
require_once('ext/ext/test.php'); 

每次包括应该注册它使用以下方法调用我们的公用工程路径文件:

$fpd->registerPath(__FILE__);

0

我相信我发现了一个anwser我自己的问题。 当我完成我的代码时,我会回来,但这是我发现的。

我知道通过使用__FILES__我会得到该代码片段存在的文件的完整目录。通过使用$_SERVER['PHP_SELF'],我将获得当前正在执行的脚本的相对于文档根目录。

通过知道我可以得到2个字符串。我可以将目录的其余部分添加到$_SERVER['PHP_SELF']

所以我可以替换看起来相像的字符串。最后,我temporarely的解决方案是这样的:

// adding the rest of the directory to $_SERVER and replacing/with \ . 
    $path_incg = str_replace('/','\\','C:/xampp/htdocs'.dirname($_SERVER['PHP_SELF']).'/'); 
// taking the parent directory of the full directory to the file. 
    $path_incd = dirname(__FILE__); 
// replacing the parts that look exactly like each other 
    self::$path = str_replace($path_incg,'',$path_incd).'\\'; 
    return self::$path; 

例1:可以说,$_SERVER['PHP_SELF']又名。包括文件是在/root/including.php并通过dirname()我们可以得到/root,并通过添加双方目录的休息和更换/ with \我们可以得到:

$var1 = c:\xampp\htdocs\root\ 

,并通过使用dirname(__FILE__)我们可以得到的目录这个文件包含在里面,让我们说它在root/inc/include.php中,那么我们得到:

$ var2 = c:\ xampp \ htdocs \ root \ inc \ include.php 我们现在可以获得目录这两个文件通过使用str_replace()

$ finalpath = str_replace($ path_incg,'',$ path_incd)。'\'; 最终结果将是:inc。如果要将包含文件放置在包含文件下方的站点地图中的任何其他位置,它将找到两个文件之间的路径。