PHP Chmod 创建文件时出现问题
我遇到以下情况:
public_html - 755
=>头像 - 777
=> poll - 755
现在,当我使用以下代码时,我会收到错误(警告:file_put_contents(../test.php)[function.file-put-contents]:无法打开流:XXX中的权限被拒绝):
<?php
file_put_contents('../test.php','<?php');
?>
但是当我使用下面的代码时,它会工作得很好:(
<?php
file_put_contents('test.php','<?php');
?>
都是从“avatar”执行的,带有0777)
我该如何解决这个问题?
I've got the following situation:
public_html - 755
=> avatar - 777
=> poll - 755
Now when I use the following code, i'll get an error (Warning: file_put_contents(../test.php) [function.file-put-contents]: failed to open stream: Permission denied in XXX):
<?php
file_put_contents('../test.php','<?php');
?>
But when I use the code below, it'll work just fine:
<?php
file_put_contents('test.php','<?php');
?>
(both executed from 'avatar', with 0777)
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的脚本是从具有 0777 权限(全局读/写/执行)的
avatar
执行的,因此您能够在其中创建文件是正常的(即:file_put_contents("test.php")
)。如果您无法在
public_html
中创建文件(即:file_put_contents("../test.php")
),这是因为正在执行脚本的用户 (最有可能的是 Apache 用户)不是public_html
的所有者(所有者很可能是 FTP 用户)。因为0755意味着只有所有者才能写入该目录,其他人只能读取或执行该目录。如果您有 shell 访问权限,则可以使用 chown 来更改文件的所有者:
或者您可以为非所有者使用更高权限的 chmod ,但是您应该小心与此。
Since your script is executing from
avatar
, which has 0777 permission (world read/write/execute), it is normal that you are able to create a file within it (i.e.:file_put_contents("test.php")
).If you are not able to create files in
public_html
(i.e.:file_put_contents("../test.php")
), it's because the user that is executing your script (most probably the Apache user) is not the owner ofpublic_html
(the owner is most probably a FTP user). Because 0755 means that only the owner is able to write to the directory, then others are only able to read or execute from it.If you have shell access, you can use
chown
to change the owner of the file:Or you can
chmod
with higher permissions for non-owners, but you ought to be careful with that.我想即使您拥有 0777 权限,也不可能写入更高的文件夹。
无法在此目录上使用 chmod,您必须使用 FTP 或其他方式。
I guess it's not possible to write to a higher folder, even when you've 0777 permission.
It's not possible to use chmod on this dir, you'll have to use FTP or something.