4

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!

lifeIsGood
  • 1,229
  • 1
  • 15
  • 22
  • 1
    It seems like the user `android.php` is running when it creates `output.php` is different than the user the other 2 are running as. Try to make all 3 run as the same system user. Is there a difference in output of `ls -l output.php` when android vs the other 2 create the file? – David Xia Feb 16 '12 at 21:59
  • Can you phrase your question in a more generalized way ? You have 3 threads/processes that need to communicate between them and you use files as queues ? what are you trying to achieve ? If you can describe that - you might get better answers, for example, using an existing design pattern that you might not be familiar with. – Nir Alfasi Feb 17 '12 at 04:13

3 Answers3

4

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);
Oli
  • 1,622
  • 18
  • 14
  • I just gave that a quick try but it didn't work. Will have a few more attempts in a few minutes. Thanks for idea – lifeIsGood Feb 16 '12 at 22:59
4

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!

Mathlight
  • 6,436
  • 17
  • 62
  • 107
lifeIsGood
  • 1,229
  • 1
  • 15
  • 22
1

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.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
  • I am not aware of how to use 'ls -l' command. I use FireFTP to connect, can i do it in there somewhere? – lifeIsGood Feb 16 '12 at 22:19
  • Try right clicking on the file and displaying more info about it. Can you add an extra column to FireFTP to display ownership? – David Xia Feb 16 '12 at 22:25
  • Yes, they are owned by different users. they are mentioned via a number, it seems 'owner' is 513 and the html post method is user '99'. the numbers stay the same. How do I put them into a 'group'? and I don't understand the g+rw via chmod comment either – lifeIsGood Feb 16 '12 at 23:01
  • Look to the docs of your FTP client if that's your interface to the remote filesystem. `chmod g+rw` sets a files' permissions such that a user in a group that owns the file may read or write to it. – quickshiftin Feb 16 '12 at 23:23
  • I am having trouble finding any such information - I also have access via cPanel if that is another way? – lifeIsGood Feb 16 '12 at 23:36