PHP and file write permissions

I have a folder with three different php scripts. email.php txt.php android.php

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

Initially, I only had an email and txt, and with that, everything was fine

$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 problem, scripts can open / write to a file, and if it does not work out, they will create it. The folder was not recorded.

Now I have an android.php script, and initially it could not open / create the output.php file until I made the entire folder writable. Now it can create output.php if necessary.

Now the problem is that either scripts can create / write to output.php HOWEVER, as soon as a file is created by one of the scripts, others cannot write it i.e. if the android script creates output.php, the email and txt scripts return errors if the email scripts or txt create output.php then the reverse script android error

error "unable to pass fopen" or something in these lines.

Lorenzo (the user here on SO) started to mention something about the users (i.e. the letter that was sent over the channel will be considered as another user in the http POST command). The scripts create output.php with permissions of 644 (without execution and only the owner can write), and I can’t manually change the permissions to 777 - although I would prefer to get a way to allow the scripts to create it, so I can delete the folder periodically for backup reasons and no need to remember to bring it back, etc.

I think I can combine 3 scenarios into one (I hope), but I would prefer not too much. Does anyone have any other ideas?

thanks

Update: since I am a new user and cannot answer my question -

Ok, so with the help of everyone who answered, I developed a solution: Email and txt scripts are run by the user "owner", and htmlPOST is run by the user "?"

I needed to make the chmod 777 folder for user '?' work

When each script is run, it checks the file 'output.php'. If this was not the case, then after fclose I added chmod 777 - thus, scripts run by other users could be opened / written later. If the file already exists, then I did not add chmod, because if it was the wrong user, it created an error. So a simple example:

 $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!

+4
source share
3 answers

Ok, so with the help of everyone who answered, I developed a solution: Email and txt scripts are run by the user "owner", and htmlPOST is run by the user "?"

I needed to make the chmod 777 folder for user '?' work

When each script is run, it checks the file 'output.php'. If this was not the case, then after fclose I added chmod 777 - thus, scripts run by other users could be opened / written later. If the file already exists, then I did not add chmod, because if it was the wrong user, it created an error. So a simple example:

 $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
source

if it's a resolution problem and you

Cannot manually change 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); 
+3
source

Use ls as @DavidXia suggests and check who owns the rights to the output files. They must belong to the user who ran some script created by them. If the files belong to other users, you will need to put the users in a common group and assign all the files belonging to this group (the users who own the files may remain discrete). Then you also want to assign g + rw all these files with chmod .

+1
source

Source: https://habr.com/ru/post/1396933/


All Articles