1

I have a cronjob.php script which computes an array and saves it in a folder

www.mywebsite.com/resoucres/array.json

cronjob.php

$array = array();

// A lot of code

file_put_contents("array.json",json_encode($array));

When I execute the script I get the

file_put_contents(array.json): failed to open stream: Permission denied in /home/user/www/mywebsite.com/resoucres/cronjob.php

Now as suggested in file_put_contents permission denied one could simply give the folder resources chmod 777. But isn't this a security risk?

Does this mean that other poeple can also write on the files in this directory? Is it possible to only give cronjob.php the rights to change the file array.json, or do I actually need to move the folder to /www/resources/cronjob.php/ so it can have chmod 777 but since noone can access it, it is save?

Community
  • 1
  • 1
Adam
  • 25,960
  • 22
  • 158
  • 247
  • Give the file group write permission and put both yourself and whatever else needs to write to the file the same group. If that doesn't make sense, start by learning about Linux user/group/other read/write/execute permissions. – kainaw May 26 '16 at 16:22
  • How about running the cron job as a user that *does* have permission to write to that folder? – Sammitch May 26 '16 at 16:56

1 Answers1

2

If you want to allow files to be written to and somewhat secure them.

Make a location outside of the web server root dir.

root dir = /opt/www/html/

Data dir = /opt/www/data/

Give the web server write access to the data dir.

Have your program wite to the data dir.

Then use a second program to read from the data dir.

That way the web server does not have direct access to the files.

Jason K
  • 1,406
  • 1
  • 12
  • 15