Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and file write permissions

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!

like image 301
lifeIsGood Avatar asked Feb 22 '23 11:02

lifeIsGood


2 Answers

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);
like image 75
Oli Avatar answered Mar 03 '23 12:03

Oli


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!

like image 21
lifeIsGood Avatar answered Mar 03 '23 11:03

lifeIsGood