具有相对路径的file_get_contents
2022-08-30 10:59:39
我有以下目录结构。
/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');