当前位置: 首页 > 工具软件 > Contents > 使用案例 >

php file_get_contents路径问题,file_get_contents与相对路径

向苗宣
2023-12-01

/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');

+0

前两个读取'detail.php',而不是'edit.json'。 –

+0

@GolezTrol。是的,这只是为了测试目的。 –

+3

最好不要使用相对目录,而是使用绝对路径,例如'__DIR__','__FILE__',设置或其他服务器配置。另请参阅:[解决PHP相对路径问题](http://yagudaev.com/posts/resolving-php-relative-path-problem/)了解如何和为什么。 –

 类似资料: