我正在尝试从主目录中的文件中获取内容,并希望避免使用tilde in path的绝对路径。
我在shell中创建了文件
echo "test" > ~/test_file.txt
然后我尝试用内容()读取文件
file_get_contents('~/test_file.txt')
获取以下PHP错误
Warning Error: file_get_contents(~/test_file.txt): failed to open stream: No such file or directory
更新
这是对https://stackoverflow.com/a/1894954/2686510的复制
我现在使用的代码片段
$home = getenv("HOME");
file_get_contents($home . DIRECTORY_SEPARATOR . 'test_file.txt');
发布于 2021-02-10 02:14:10
您的问题的答案是在这里https://stackoverflow.com/a/1894954/2686510
简而言之,使用$_SERVER['HOME']
变量访问当前用户主文件夹。
https://stackoverflow.com/questions/66134618
复制