我有以下目錄結構。file_get_contents與相對路徑
/var/www/base/controller/detail.php
/var/www/base/validate/edit.json
/var/www/html
在/var/www/base/controller/detail.php
,我怎麼使用file_get_contents()
使用相對路徑來讀取/var/www/base/validate/edit.json
?我已經試過如下:
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('detail.php');
//No error, but I don't want this file and was just testing
$json=file_get_contents('detail.php', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('./validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('././validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../../validate/edit.json', FILE_USE_INCLUDE_PATH);
//This works, but I want to use a relative path
$json=file_get_contents(dirname(dirname(__FILE__)).'/validate/edit.json');
前兩個讀取'detail.php',而不是'edit.json'。 – GolezTrol
@GolezTrol。是的,這只是爲了測試目的。 – user1032531
最好不要使用相對目錄,而是使用絕對路徑,例如'__DIR__','__FILE__',設置或其他服務器配置。另請參閱:[解決PHP相對路徑問題](http://yagudaev.com/posts/resolving-php-relative-path-problem/)瞭解如何和爲什麼。 – GolezTrol