Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel fopen() cant create/read file

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

like image 992
Dimitrios Stefos Avatar asked Dec 04 '22 21:12

Dimitrios Stefos


1 Answers

You are working with storage folder but by default the path takes from public that why you need to use ../
Try like this

$file=fopen('../storage/app/test.json','w');
fwrite($file,json_encode($line));
fclose($file);
like image 147
Bibhudatta Sahoo Avatar answered Dec 11 '22 15:12

Bibhudatta Sahoo