Why can't PHP open this file?

I’m working with an API that gives JSON. I want to save the contents of the API request to a file called cache.json, but it seems I don’t have the permissions.

  $f = fopen("cache.json", "w") or die("Failed to open file");
  fwrite($f, $output);

I made sure to make cache.json owned by www-data, but that didn’t seem to work.

$ ls -l
total 136
-rwxrwxrwx 1 www-data www-data    0 Feb 24 09:39 cache.json

Error log:

PHP Warning:  fopen(cache.json): failed to open stream: Permission denied in /var/www/flag/server/abuseipdb.php on line 19

How can I fix this?

php is bad, use Node.js instead
I’m assuming you are using a linux computer with root access so can www-data access the folder and not just the file?

1 Like