Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents to get file in your own local machine

Tags:

php

php-5.3

I am currently testing my code on my local machine and I wanted it to be able to read and write to a text file I have so I have the following code:

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

Here's the code that I have:

$file = '~/Desktop/insta_user.txt';
$current = file_get_contents($file, FILE_USE_INCLUDE_PATH);

The file already exists on my desktop, just wondering how I can write to this file locally.

like image 681
adit Avatar asked Dec 15 '22 06:12

adit


1 Answers

Use a complete path:

$file = '/Users/yourname/Desktop/insta_user.txt';
$current = file_get_contents($file);

Even if the webserver is running as your user it might not have the same environment.

like image 165
calcinai Avatar answered Jan 29 '23 01:01

calcinai