Laravel fopen() cant create/read file

Viewed 26204

so I have this Laravel project and I want to export an array to a json file to use it later. My permissions on the storage folder are 777 but when I do

$line=[
  'key1'=>'value1',
];
file_put_contents("storage/app/test.json",json_encode($line));

I also tried

$line=[
  'key1'=>'value1',
];
file_put_contents($_SERVER['DOCUMENT_ROOT']."/storage/app/test.json",json_encode($line));

and in both cases (plus some more) I get this error

file_put_contents(storage/app/test.json): failed to open stream: No such file or directory

Do you have any idea why is this happening?

EDIT: The folders exist

4 Answers

You can use Laravel's helper method base_path():

The base_path function returns the fully qualified path to the project root.

Eg:

$fp = fopen(base_path() . 'app/Encryption/PrivateKeys/nginx-selfsigned.key','r');
Related