tags:

views:

57

answers:

1

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);

}

+2  A: 

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 :

chmod($count_my_page, 0666);

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) :

There are three specific permissions on Unix-like systems that apply to each class:

  • The read permission, which grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory (but not to find out any further information about them, including file type, size, ownership, permissions, etc.)
  • The write permission, which grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
  • The execute permission, which grants the ability to execute a file. This permission must be set for executable binaries (for example, a compiled c++ program) or shell scripts (for example, a Perl program) in order to allow the operating system to run them. When set for a directory, this permission grants the ability to traverse its tree in order to access files or subdirectories, but not see files inside the directory (unless read is set).


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

Pascal MARTIN
That was a great help, thanks. At first I wasn't sure where you meant to put that, but I moved it around a bit until it ended up right below the fpopen.
pedalpete
You're welcome :-) (btw, I never really know where to put it either, but I generally end up putting the chmod call after the fclose ; that way, I'm sure everything's done with the file)
Pascal MARTIN
Even though this is the accepted answer, I have a problem with it: The web server user should be the file owner, and unless the server has a really weird umask, will already have read and write permissions to it.
R. Bemrose
Actually, you are quite right, and I'm now asking myself the same question :-D (at first, I didn't really notice the creation of the file and the access problem were both under Apache -- odd)
Pascal MARTIN