PHP 和文件写入权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9319619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:33:21  来源:igfitidea点击:

PHP and file write permissions

phppermissions

提问by lifeIsGood

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

我有一个文件夹,里面有 3 个不同的 php 脚本。电子邮件.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.

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

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

本来我只有email和txt,用这个就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.

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

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

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

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

错误是“无法流式传输 fopen”或类似的内容。

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

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

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

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

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

好的,在所有回复的人的帮助下,我找到了一个解决方案:电子邮件和 txt 脚本由用户“所有者”运行,而 htmlPOST 由用户“?”运行。

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

我必须为用户“?”创建文件夹 chmod 777 上班

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:

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

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

谢谢你的帮助!

采纳答案by lifeIsGood

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

好的,在所有回复的人的帮助下,我找到了一个解决方案:电子邮件和 txt 脚本由用户“所有者”运行,而 htmlPOST 由用户“?”运行。

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

我必须为用户“?”创建文件夹 chmod 777 上班

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:

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

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

谢谢你的帮助!

回答by Olivier

if it is a permission problem and you are

如果这是一个权限问题而你是

unable to manually change the permissions to 777

无法手动将权限更改为 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);

回答by quickshiftin

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+rwto all those files via chmod.

按照@DavidXia 的建议使用 ls 并检查谁拥有输出文件的所有权。它们应该归运行创建它们的任何脚本的用户所有。如果文件由不同的用户拥有,那么您需要将用户放在一个公共组中,并将所有文件分配给该组(拥有文件的用户可以保持离散)。然后,您还需要通过chmodg+rw分配给所有这些文件。