file_get_contents在您自己的本地计算机中获取文件

adi*_*dit 2 php php-5.3

我目前正在我的本地机器上测试我的代码,我希望它能够读取和写入我拥有的文本文件,所以我有以下代码:

Warning: file_get_contents(~/Desktop/insta_user.txt): failed to open stream: No such file or directory in /Users/Brad/Sites/App/src/App/MainBundle/Controller/InstaController.php on line 33
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

$file = '~/Desktop/insta_user.txt';
$current = file_get_contents($file, FILE_USE_INCLUDE_PATH);
Run Code Online (Sandbox Code Playgroud)

该文件已存在于我的桌面上,只是想知道如何在本地写入此文件.

cal*_*nai 10

使用完整路径:

$file = '/Users/yourname/Desktop/insta_user.txt';
$current = file_get_contents($file);
Run Code Online (Sandbox Code Playgroud)

即使Web服务器以您的用户身份运行,它也可能没有相同的环境.