views:

32

answers:

2

I'm using a simple fopen("w") and fwrite setup to write a new file on my server. Recently, this has been arbitrarily assigning 0640 permissions to these files, which means I can't view them from a browser. But it's not happening every time. Can anybody tell me why? What I'm missing here? Here's my code:

if ($file=fopen("$filesDir/$yr/$mo/{$tempFile[0]}.xml","wb")) {
    if(fwrite($file,$output,strlen($output))) {
        $success="YES";
        $message="File successfully built.";
    } else {
        $message="Could not write XML file.";
    }
} else {
    $message="Could not open file.";
}
+2  A: 

I would venture to guess that it might have something to do with the umask, or the current permissions of the directory that you are creating the file in. Try adding sticky groups to the directory and keeping the group www-data or apache (depending on your install)

Ascherer
Would that cause files to have different permissions seemingly randomly, despite being in the same folder?
Ben Saufley
Why it would be setting them to seeming different permissions is beyond me, but if you give it something to go by, I don't think it would be random anymore.
Ascherer
OK, then … hahah, this may sound silly, but how do I go about doing that?
Ben Saufley
been awhile since ive done this, might be wrong but, try thisfind ./nameofdir -type d -exec sudo chmod g+s {} \;sudo chgrp -R www-data nameofdir
Ascherer
I tried that verbatim, with my directory instead of nameofdir of course, and it's just saying Invalid Command. I'm just doing this through OSX's Terminal... Also I'm not entirely sure what that stuff means. I mean I know chmod, and I've seen sudo before, but that's about it.
Ben Saufley
ahh, see i assumed u were using ubuntu or something. im not sure how to do that on Terminal, i would have to say the best method now would just be to chmod it after u create itsadly i do not own a mac :/
Ascherer
I'm using Terminal to get into what I believe is an Apache server elsewhere. The strange thing is, i was having this problem somewhere else and I decided to just chmod after writing the file every time, as a lazy shortcut, and that apparently isn't working now either.
Ben Saufley
id check to make sure everything has a group ownership by apache
Ascherer
As far as I understand it the folder in which all of this is taking place has rwxrwxr-- ownership (774). I've got it now so that as soon as it's built the file it automatically chmods the file so that it has the proper permissions, and that is STILL not doing the trick. Any thoughts? This is driving me crazy.
Ben Saufley
so they are still getting created with 640 and chmod isnt doing anything?
Ascherer
It looks like I was actually coding the chmod wrong, so that solves that problem. I was trying to chmod the string by accident. So I think that's the easiest workaround. But I'm still annoyed that I can't figure out what was causing the problem in the first place.
Ben Saufley
my guess is its some sort of umask problem, but i dont know a lot about the system ur running on/with
Ascherer
A: 

It does this by default because PHP assumes that you are going to read the file again. Or, in this case write to it. You might be able to change the default settings in the php.ini file.

alexy13