2014-02-23 283 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。如果要將包含文件放置在包含文件下方的站點地圖中的任何其他位置,它將找到兩個文件之間的路徑。