【问题标题】:file_put_contents saying permission denied? [duplicate]file_put_contents 说权限被拒绝? [复制]
【发布时间】:2011-08-24 19:15:26
【问题描述】:

可能重复:
file_put_contents permission denied

我最近转移了服务器,并且似乎 file_put_contents 在新服务器上不起作用。

一切都一样,文件夹已正确修改,但由于某种原因,它没有创建文件并将内容放入其中。

我创建了一个测试供您查看,模拟我们目前的做法:

file_put_contents("/home/user/public_html/test/test.progress", "test");

脚本正在运行

/home/user/public_html/test.php

/test folder is chmodded to 755 (777 makes no difference)

我收到以下错误:

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

我是否需要更改服务器上的任何设置才能使其正常工作?怎么了?

【问题讨论】:

  • 需要更多关于环境的信息。
  • 服务器运行的用户可能与文件夹 /test 的创建者不同
  • Cent OS,linux,服务器版本:Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4 PHP/5.3.6
  • 试试file_put_contents($file, $content, FILE_APPEND | LOCK_EX);

标签: php io


【解决方案1】:

您可能使用了错误的用户。 检查 PHP 是否使用拥有您尝试写入的目录的同一用户。 PHP 经常使用 www-data(www-data 是 Ubuntu 上的 web 服务器使用的用户,例如 Apache 和 Nginx))所以如果目录被 chmodded 为 755,这意味着创建目录的用户可以写入它,但是其他人只能阅读。 chown 到 php 用户或 chmod 到 777。

我个人运行 PHP fastcgi,它以唯一用户运行,所以我没有这个问题,考虑切换到 fastcgi。

【讨论】:

  • 这也适用于文件 test.progress。如果该文件已经存在,请确保它也可由 PHP 用户写入。
  • 我的是这个问题! - mamp pro、apache/web 等在 www/mysql 下运行 - 而不是 name/name :)
猜你喜欢
  • 1970-01-01
  • 2014-04-10
  • 2018-04-25
  • 2011-06-22
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
相关资源
最近更新 更多