[英]PHP - How do I save the path of a directory?
好的,可以說我包含名為/ext/include.php的目錄中的文件(請參見示例1)
我想要“ include.php”文件中的一些代碼來找出路徑並保存。 因此示例1的路徑設置為:/ ext /
該代碼必須有效,因此,如果我通過2個目錄包含文件(請參見示例2)。 它只是將路徑保存為:/ ext / ext /
我嘗試使用類似__FILE__
,但保存了整個目錄。 我只希望包含文件的文件和包含文件的文件之間的路徑。
希望您能幫忙。 :-)請嘗試使其保持oop。 我正在學習。
如果您可以同時提供oop和程序示例,那就太好了!
范例1:
|index.php
|+ext
| include.php
范例2:
|index.php
|+ext
| +ext
| include.php
更新/解決
我無法回答我的問題8個小時了,太好了。
This is my temporarly soloution:
我相信我對自己的問題感到惱火。 代碼寫完后,我會回來的,但這是我發現的。
我知道通過使用__FILES__
可以獲取代碼所在文件的完整目錄。通過使用$_SERVER['PHP_SELF']
可以獲取當前正在執行的腳本相對於文檔根目錄的目錄。
通過知道我可以得到2個字符串,我可以比較它們。 而且我可以將目錄的其余部分添加到$_SERVER['PHP_SELF']
。
因此,我可以替換看起來彼此相似的字符串。 最后,我的臨時解決方案如下所示:
// 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 \\
替換/ 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\\
。 如果將包含文件放置在站點地圖中包含文件下方的任何其他位置,它將找到這兩個文件之間的路徑。
假定包含文件始終存在於目錄結構中的主文件下,則應執行以下操作。
<?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__);
我相信我對自己的問題感到惱火。 代碼寫完后,我會回來的,但這是我發現的。
我知道通過使用__FILES__
可以獲取代碼所在文件的完整目錄。通過使用$_SERVER['PHP_SELF']
可以獲取當前正在執行的腳本相對於文檔根目錄的目錄。
知道我可以得到2個字符串。 而且我可以將目錄的其余部分添加到$_SERVER['PHP_SELF']
。
因此,我可以替換看起來彼此相似的字符串。 最后,我的臨時解決方案如下所示:
// 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 \\
替換/ 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。 如果將包含文件放置在站點地圖中包含文件下方的任何其他位置,它將找到這兩個文件之間的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.