apache 使用 PHP 写入目录:这是权限问题吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/678388/
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
Write to Directory using PHP: Is this a permissions problem?
提问by Abs
My PHP script writes to a file so that it can create a jpg image.
我的 PHP 脚本写入一个文件,以便它可以创建一个 jpg 图像。
fwrite($handle, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($handle);
print $newfile.'.jpg';
I have put this script on a new server however the image never gets saved. The permission of the folder it saves to is 755 but it does not own it. Last time, I think I fixed the issue by changing the directory owner to apache as this is what PHP runs as. I can not do the same again because I am not root.
我已将此脚本放在新服务器上,但图像从未保存。它保存到的文件夹的权限是 755 但它不拥有它。上次,我想我通过将目录所有者更改为 apache 来解决这个问题,因为这是 PHP 运行的方式。我不能再做同样的事情,因为我不是 root。
Firstly., Is there another fix? Secondly, If I could change the owner of the directory like last time will this fix the issue?
首先,还有其他修复吗?其次,如果我可以像上次一样更改目录的所有者,这会解决问题吗?
Thanks all for any help
感谢大家的帮助
回答by vartec
- change permissions of the folder to 777 (
rwxrwxrwx) - from PHP create subdirectory with mkdir
- change your directory permissions back to 755 (
rwxr-xr-x) or even better 711 (rwx--x--x) - from now on save your images in the subdirectory, which is now owned by www-data (or whichever user Apache uses).
- 将文件夹的权限更改为 777 (
rwxrwxrwx) - 从 PHP 使用mkdir创建子目录
- 将您的目录权限改回 755 (
rwxr-xr-x) 或更好的 711 (rwx--x--x) - 从现在开始,将您的图像保存在子目录中,该目录现在归 www-data(或 Apache 使用的任何用户)所有。
BTW. you can reduce following code:
顺便提一句。您可以减少以下代码:
fopen($newfile.'.jpg','wb');
fwrite($handle, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($handle);
print $newfile.'.jpg';
to:
到:
file_put_contents($newfile.'.jpg',
file_get_contents('php://input',
FILE_BINARY),
FILE_BINARY)
回答by Powerlord
If you're not the owner, then yes, the directory being 755 is a problem.
如果您不是所有者,那么是的,目录 755 是一个问题。
To break it down, 755 is:
分解一下,755是:
- user (owner) has read (4), write (2), and execute (1)
- group has read (4) and execute (1)
- others have read (4) and execute (1)
- 用户(所有者)已读取 (4)、写入 (2) 和执行 (1)
- 组已读取 (4) 并执行 (1)
- 其他人已阅读 (4) 并执行 (1)
Notice that onlythe owner has write privileges with 755.
请注意,只有所有者拥有 755 的写入权限。
P.S. fopen should return an error if the open failed.
PS 如果打开失败, fopen 应该返回一个错误。
回答by opoE
use this:
用这个:
>chown dir_name www-data
回答by Seb
I had a similar problem a couple of weeks ago. It ended up being the PHP safe mode set in the server.
几周前我遇到了类似的问题。它最终成为服务器中设置的 PHP 安全模式。
Check that out!
检查出!
回答by L. Cosio
Put error_reporting(E_ALL) at the top of your script and check if it prints out any alert while trying to save an image.
将 error_reporting(E_ALL) 放在脚本的顶部,并检查它在尝试保存图像时是否打印出任何警报。

