2013-07-02 291 views
0

我運行XAMPP,我有一個簡單的網站結構:相對路徑不工作

- > C:\ XAMPP \ htdocs中\ mywebsite \酒吧\的index.php

- - > C:\ XAMPP \ htdocs中\ mywebsite \ other.php

中的index.php我

<?php require("../other.php"); ?> 

,當我瀏覽到index.php我看到下面的錯誤:

Warning: require(../other.php): failed to open stream: No such file or directory in C:\xampp\htdocs\mywebsite\pub\index.php on line 2

Fatal error: require(): Failed opening required '../other.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\mywebsite\pub\index.php on line 2

不會「../」帶我回到上一個目錄嗎?它工作正常,如果我在網上運行的代碼,但不是本地

**我更感興趣,爲什麼它不能在本地工作,而不是一個解決方案,因爲問題與一個項目,我必須改變很多的路徑手動只是爲了得到它的工作在當地

回答

0

嘗試:

<?php require($_SERVER['DOCUMENT_ROOT']."/other.php"); ?> 
+0

當我回聲$ _SERVER ['DOCUMENT_ROOT']我得到C:/ xampp/htdocs ...看起來像我需要改變根。親戚有沒有任何理由不工作?這不就是相對於調用require的文件嗎? –

0

這肯定會工作。

<?php require( realpath('/../'. basename(__FILE__)) . DIRECTORY_SEPARATOR "other.php"); ?> 

OR

<?php require(realpath('/../'. __DIR__) . DIRECTORY_SEPARATOR "other.php"); ?> 

這裏 basename(__FILE__)__DIR__屬於當前目錄。