簡體   English   中英

PHP動態獲取根目錄絕對路徑

[英]PHP obtain root dir absolute path dynamically

我是php的新手。

我想創建一個php配置文件,用於設置項目目錄結構的絕對路徑。 我的配置文件位於“ private / class”目錄中,它將包含在“ root”和私有目錄文件中。

項目目錄結構為

class
img
photo
etc
private/class
private/photo
private/photo/thumb
private/etc

為了獲得文件所在的絕對路徑,我使用了神奇的php costants DIR

define('MAINPATH', __DIR__ );

這個節目

/var/www/projectname/private/class

如果我將配置文件包含在index.php或private / index.php等中,則此變量保持不變。

現在,我遇到了從主路徑開始尋找另一條路徑的問題。 通常,我使用realpath,但是在這種情況下,我認為效果不會很好。 我以這種方式嘗試

define('PHOTOPATH', __DIR__ . '../photo');

define('PHOTOPATH', realpath ( __DIR__ . '../photo'));

這些節目

1 - /var/www/projectname/private/class../photo
2 - nothing

我該怎么辦? 謝謝

您只需要在添加路徑的其余部分之前添加PATH分隔符( / )。

define('PHOTOPATH', realpath ( __DIR__ . '/../photo'));
                                         ^
                                         |
                                         +- notice here the '/' is added

您可以使用$ _SERVER ['DOCUMENT_ROOT']。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM