PHP and file write permissions

2020-02-11 07:18发布

I have a folder with 3 different php scripts in it. email.php txt.php android.php

I pipe emails and/or txts to their respective scripts, and use http POST to send data to the android script.

Originally I only had the email and txt, and using this is was all ok

$data = '//data fields condensed into a single string obtained from email or txt';
$filename= "output.php";
$newFile= fopen($filename, 'w+');
fwrite($newFile, $data);
fclose($newFile);`

No problems, the scripts could open/write to the file, and if it didn't exits they would create it. the folder was not writeable.

Now I have the android.php script and originally it could NOT open/create the output.php until I made the whole folder writeable. Now it can create output.php if need be.

The problem now, is that either scripts can create/write to output.php HOWEVER, once the file is created by one of the scripts, the other ones cannot write to it Ie if the android script creates output.php, the email and txt scripts return errors if the email or txt scripts create output.php then the android script return error

the error being 'unable to stream fopen' or something along those lines.

Lorenzo (user here on SO) started to mention something about the users (ie the email being piped would be seen as a different user to an http POST command). The scripts create output.php with permissions 644 (no execute and only owner can write) and I am unable to manually change the permissions to 777 - although I would prefer to get a way that allows the scripts to create it - so I can empty the folder periodically for backup reasons and not have to remember to put it back in etc...

I am thinking I could merge the 3 scripts into one (hopefully) but would prefer not too. Does anyone have any other ideas?

Thanks

Update: Since I am a new user and couldn't 'answer' my own question -

Ok, so with the help of everyone who responded, I have worked out a solution: The email and txt scripts are run by user 'owner' and the htmlPOST is run by user '?'

I had to make the folder chmod 777 in order for user '?' to work

When each script runs, it checks for the file 'output.php'. If it didn't exist, then after the fclose I added a chmod 777 - that way the scripts run by other users could open/write it later. If the file already existed, then I didn't add the chmod because if it was the wrong user it created an error. So a simple example of it:

$data = '//data fields condensed into a single string obtained from email or txt';
$filename= "output.php";

if (file_exists($filename)){
$newFile= fopen($filename, 'w+');
fwrite($newFile, $data);
fclose($newFile);
} else {
$newFile= fopen($filename, 'w+');
fwrite($newFile, $data);
fclose($newFile);
chmod($file, 0777);
}

Thanks for your help!

3条回答
等我变得足够好
2楼-- · 2020-02-11 07:51

Ok, so with the help of everyone who responded, I have worked out a solution: The email and txt scripts are run by user 'owner' and the htmlPOST is run by user '?'

I had to make the folder chmod 777 in order for user '?' to work

When each script runs, it checks for the file 'output.php'. If it didn't exist, then after the fclose I added a chmod 777 - that way the scripts run by other users could open/write it later. If the file already existed, then I didn't add the chmod because if it was the wrong user it created an error. So a simple example of it:

$data = '//data fields condensed into a single string obtained from email or txt';
$filename= "output.php";

if (file_exists($filename)){
    $newFile= fopen($filename, 'w+');
    fwrite($newFile, $data);
    fclose($newFile);
} else {
    $newFile= fopen($filename, 'w+');
    fwrite($newFile, $data);
    fclose($newFile);

    chmod($filename, 0777);
}

Thanks for your help!

查看更多
乱世女痞
3楼-- · 2020-02-11 07:52

Use ls as @DavidXia suggests and check who has ownership of the output files. They should be owned by the user who was running whichever script created them. If the files are owned by different users, then you will need to put the users in a common group and assign all the files to be owned by that group (the users who own the files can remain discrete). Then you'll also want to assign g+rw to all those files via chmod.

查看更多
放我归山
4楼-- · 2020-02-11 07:54

if it is a permission problem and you are

unable to manually change the permissions to 777

maybe you could try:

$filename= "output.php";

// programatically set permissions
if(!file_exists($filename)){
    touch($filename);
    chmod($filename, 0777);
}

$data = '//data fields condensed into a single string obtained from email or txt';
$newFile= fopen($filename, 'w+');
fwrite($newFile, $data);
fclose($newFile);
查看更多
登录 后发表回答