php – 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');
你有没有尝试过:
$json = file_get_contents(__DIR__ . '/../validate/edit.json');
为什么,见http://yagudaev.com/posts/resolving-php-relative-path-problem/
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |