php file_put_contents 说权限被拒绝?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5989984/
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
file_put_contents saying permission denied?
提问by Latox
Possible Duplicate:
file_put_contents permission denied
I have recently transferred server, and it seems that file_put_contents is not working on the new server.
我最近转移了服务器,似乎 file_put_contents 在新服务器上不起作用。
Everything is the same, the folders are chmodded correctly, but for some reason it is not creating the files and putting the contents in it.
一切都一样,文件夹被正确修改,但由于某种原因,它没有创建文件并将内容放入其中。
I have created a test for you to view, emulating how we are currently doing it:
我已经创建了一个测试供您查看,模拟我们目前的做法:
file_put_contents("/home/user/public_html/test/test.progress", "test");
the script is being ran at
脚本正在运行
/home/user/public_html/test.php
/test folder is chmodded to 755 (777 makes no difference)
I am getting the following error:
我收到以下错误:
Warning: file_put_contents(/home/user/public_html/test/test.progress) [function.file-put-contents]: failed to open stream: Permission denied in /home/user/public_html/test.php on line 2
Do I need to change any settings on the server for this to work? What is wrong?
我是否需要更改服务器上的任何设置才能使其正常工作?怎么了?
回答by Adam Arold
You are probably using the wrong user. Check if PHP uses the same user that owns the directory you are trying to write. PHP often uses www-data (www-data is the user that web servers on Ubuntu use such as Apache and Nginx for example)) so if the directory is chmodded to 755 it means that the user created the directory can write to it, but others can only read. chown to the php user or chmod to 777.
您可能使用了错误的用户。检查 PHP 是否使用拥有您尝试写入的目录的同一用户。PHP 经常使用 www-data(www-data 是 Ubuntu 上的 web 服务器使用的用户,例如 Apache 和 Nginx))所以如果目录被 chmodded 为 755,则意味着创建目录的用户可以写入它,但是其他人只能阅读。chown 给 php 用户或 chmod 给 777。
I personally run PHP fastcgi, it runs with an unique user so I don't have this problem, think about switching to fastcgi.
我个人运行 PHP fastcgi,它以唯一用户运行,所以我没有这个问题,考虑切换到 fastcgi。