I’ve run into an issue moving onto my test (and tried it on prod, no go) where I have a php script create a file and put some data in that file.
I’ve set the permissions to R, W, X for “Owner, Group, Other” using winSCP (i’m not much for commandline yet).
What i can see is that the script is able to create the file (i’ve deleted, and it recreates the files properly every time), but the exact same script can’t write to the file and I get a permission error.
“failed to open stream: permission denied”
Here’s the script that I’m using,it worked in windows, but now on linux, no go.
Any ideas?
$type='get';
$count_my_page = ("list".$counterDate.".txt");
if(!file_exists($count_my_page)){
$fp=fopen($count_my_page, "w");
$putArray=array($type=>'1');
$putJson=json_encode($putArray);
fputs($fp, $putJson);
fclose($fp);
} else {
$hits = file($count_my_page);
if(empty($hits)){
$putArray=array($type=>'1');
$putJson=json_encode($putArray);
} else {
$putArray=json_decode($hits[0], true);
if(!array_key_exists($type, $putArray)){
$putArray[$type] = '1';
} else {
$hit=$putArray[$type];
$putArray[$type]++;
}
}
$putJson=json_encode($putArray);
$fp=fopen($count_my_page, "w");
fputs($fp, $putJson);
fclose($fp);
}
Creating a file requires write permission on the directory (that is probably what you set with winscp ?).
But modifying a file requires write permissions on the file itself.
To give such permissions to Apache, you might have to use the function chmod after you’re done creating the file.
Something like this might do, I suppose :
6 = 4 (read) + 2 (write).
You don’t need to give execution (1) privilegies.
Does this help ?
And I supposed it worked on windows because Apache run as your user (or as administrator) — or because Windows’ permission system is more permissive
Edit : for more details about permissions under Linux, you might what to take a look at this part of the corresponding Wikipedia article (quoting) :
You could also have fun with
umask, but I’ve always prefered calling chmod when it’s necessary (and only when it’s necessary : I prefer not giving too much permissions — more secure this way) — and umask may have some problems with some servers, if I remember correctly