PHP 和文件写入权限

2022-01-04 00:00:00 permissions php

我有一个文件夹,里面有 3 个不同的 php 脚本.email.php txt.php android.php

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

我将电子邮件和/或 txt 文件通过管道传输到各自的脚本,并使用 http POST 将数据发送到 android 脚本.

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

本来我只有email和txt,用这个就OK了

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.

现在我有了 android.php 脚本,最初它无法打开/创建 output.php,直到我使整个文件夹可写.现在它可以根据需要创建 output.php.

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.

现在的问题是,任一脚本都可以创建/写入 output.php 但是,一旦文件由其中一个脚本创建,其他脚本就无法写入即如果 android 脚本创建 output.php,则电子邮件和 txt 脚本返回错误如果电子邮件或 txt 脚本创建 output.php,则 android 脚本返回错误

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

无法流式传输 fopen"或类似内容的错误.

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

Lorenzo(SO 上的用户)开始提及有关用户的一些信息(即,通过管道传输的电子邮件将被视为与 http POST 命令不同的用户).脚本创建的 output.php 权限为 644(不能执行,只有所有者可以写入),我无法手动将权限更改为 777 - 尽管我更希望获得一种允许脚本创建它的方法 - 所以我可以清空出于备份原因定期文件夹,而不必记住将其放回等...

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...

我想我可以将 3 个脚本合并为一个(希望如此),但我也不想.有人有其他想法吗?

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

谢谢

更新:由于我是新用户,无法回答"我自己的问题 -

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

好的,在所有回复的人的帮助下,我想出了一个解决方案:email 和 txt 脚本由用户 'owner' 运行,htmlPOST 由用户 '?' 运行

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 '?'

为了用户?",我必须创建文件夹 chmod 777上班

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

当每个脚本运行时,它会检查文件output.php".如果它不存在,那么在 fclose 之后我添加了一个 chmod 777 - 这样其他用户运行的脚本可以稍后打开/编写它.如果文件已经存在,那么我没有添加 chmod 因为如果它是错误的用户,它会创建一个错误.举个简单的例子:

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);
}

感谢您的帮助!

推荐答案

好的,在所有回复的人的帮助下,我找到了一个解决方案:email 和 txt 脚本由用户 'owner' 运行,htmlPOST 由用户 '?' 运行

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 '?'

为了用户?",我必须创建文件夹 chmod 777上班

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

当每个脚本运行时,它会检查文件output.php".如果它不存在,那么在 fclose 之后我添加了一个 chmod 777 - 这样其他用户运行的脚本可以稍后打开/编写它.如果文件已经存在,那么我没有添加 chmod 因为如果它是错误的用户,它会创建一个错误.举个简单的例子:

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);
}

感谢您的帮助!

相关文章