PHP和文件写入权限 [英] PHP and file write permissions

查看:723
本文介绍了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.

最初我只有电子邮件和txt,并且可以使用

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命令不同的用户).脚本创建具有权限644(无执行且只有所有者可以写)的output.php,并且我无法手动将权限更改为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 -

好吧,因此在所有做出回应的人的帮助下,我制定了一个解决方案: 电子邮件和txt脚本由用户所有者"运行,而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'.如果它不存在,那么在关闭之后,我添加了一个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);
}

感谢您的帮助!

推荐答案

好,因此,在所有做出回应的人的帮助下,我制定了一个解决方案: 电子邮件和txt脚本由用户所有者"运行,而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'.如果它不存在,那么在关闭之后,我添加了一个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);
}

感谢您的帮助!

这篇关于PHP和文件写入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆