2016-09-06 105 views
0

好吧,我有一個情況,我有不同目錄中的幾個文件,需要弄清楚如何將它們連接在一起。下面是結構:包含來自其他文件的配置和數據庫

  • /home/username/public_html/index.php
  • /home/username/public_html/secure/index.php
  • /家/用戶名/的public_html /元件/具備/ db.php中
  • /home/username/config.ini

好了,在我的兩個文件的index.php我有這個包括引用db.php中:

的index.php

<?php include("elements/includes/db.php"); ?> 

安全/ index.php文件

<?php include("../elements/includes/db.php"); ?> 

現在db.php文件裏面,我有以下引用的config.ini:

$config = parse_ini_file('../../../config.ini'); 

我知道它沒有選擇,因爲它應該是相對於index.php而不是db.php,但我將如何引用這些文件correctl ÿ?我想我的config.ini在public_html目錄之外。

回答

0

使用$_SERVER['DOCUMENT_ROOT'],這樣你就不會需要弄清楚每個文件的相對路徑:

$config = parse_ini_file($_SERVER['DOCUMENT_ROOT'].'/../config.ini'); 
+0

ahhh,我不知道你可以從文檔根目錄調用返回...我認爲這是使用時的最終位置...謝謝!我會試一試。 –

+0

警告:parse_ini_file(/ home/username/public_html ./../ config.ini):無法打開流<---無法讓它引用一個目錄......嘗試了幾個變體:($ _SERVER [ 'DOCUMENT_ROOT']'../config.ini') –

+0

奇怪的是它在'public_html'後面加了額外的時間段 – aynber

0

另一種方法是使用例如魔術常量__DIR__,參見Predefinied Constants

. 
├── config.ini 
└── public_html 
    ├── elements 
    │   └── includes 
    │    └── db.php 
    ├── index.php 
    └── secure 
     └── index.php 

的public_html /元件/具備/ db.php中

<?php 

$config = parse_ini_file(
    __DIR__ . '/../../../config.ini' 
); 

的public_html/index.php的

<?php 

include __DIR__ . '/elements/includes/db.php'; 

的public_html /安全/ index.php的

<?php 

include __DIR__ . '/../elements/includes/db.php'; 

注意:我建議使用require而不是include,請參見require