【问题标题】:fopen() - permission denied - despite having correct permissions (as far as I can tell)fopen() - 权限被拒绝 - 尽管有正确的权限(据我所知)
【发布时间】:2012-08-31 06:30:53
【问题描述】:

最近移动服务器并尝试设置所有内容。我的 memcache 类会写入日志,这是我开发时需要的。

编辑:运行 Ubuntu 12.04 的服务器。

webroot 是 /var/www/public_html

日志文件位于 /var/www/logs/

这里是目录权限

-rw-r--r-- 1 root root       1415 Sep  6 11:53 default
drwxrwxr-x 2 root webmasters 4096 Sep  6 12:48 logs
drwxrwxr-x 8 root webmasters 4096 Sep  6 12:32 public_html

这是来自 /var/www/logs 的权限

-rw-rw-r-- 1 root webmasters 116103 Sep  6 13:02 access.log
-rw-rw-r-- 1 root webmasters  16094 Sep  6 12:48 error.log
-rw-rw-r-- 1 root webmasters      0 Sep  6 12:48 memcache.log
-rw-rw-r-- 1 root webmasters   9861 Sep  6 13:02 php_log.log

浏览器访问的文件是/var/www/public_html/index.php

-rw-rw-r-- 1 root webmasters 5660 Sep  6 01:51 /var/www/public_html/index.php

索引包括 /var/www/public_html/includes/cache.php

-rw-rw-r-- 1 root webmasters 4602 Sep  6 12:47 /var/www/public_html/includes/cache.php

小组成员:

grep 'webmasters' /etc/group
webmasters:x:1002:sftp_chris,www-data

我什至尝试将 /logs 和 /memcache.log 更改为 chmod 777。

不是很明白为什么会这样,因为你可以看到 php 正在将其日志文件写入 /logs/php.log 没有问题。

有什么想法吗?我真的很困惑。

我认为可能与 php-suhosin 有关,但在那里找不到任何东西。

功能:

function write_to_log($string){

    $file = '/var/www/memcache.log';
    $fh = fopen($file, 'a');
    $contents = @fread($fh, filesize($file));
    $stringData = $contents."[".date('H.i:s d-m-y')."] ".$string;
    fwrite($fh, $stringData);
    fclose($fh);
}

【问题讨论】:

  • 我对你在 PHP func 中的路径错误吗?你告诉你的日志在/var/www/public_html/,但你正试图写信给/var/www/
  • @zysoft,为了给他一个公道,他告诉他的登录是/var/www/logs,而不是/var/www/public_html
  • 这是引用 - the log file is in /var/www/public_html。也许应该编辑这个问题。这有点令人困惑,但并没有什么不同。路径仍然错误:)
  • 哎呀,是的,@zysoft,对不起 ;-)
  • 哇哇感觉有点傻。浪费了一个半小时摆弄权限并拔掉我的头发! @zysoft 日志在 /www/logs 中,但我在问题中写错了。虽然现在修好了。 :)

标签: php apache fopen chmod


【解决方案1】:

您写信给/var/www/memcache.log,而不是您显示权限的/var/www/logs/memcache.log

(为什么在写日志之前先读日志?)

【讨论】:

  • 哇哇。我花了很长时间摆弄权限,这就是全部:s。显然工作太累了。为毫无意义的问题道歉!有没有办法简单地附加到文件?老实说并没有真正抬头,我认为它必须被阅读并附加到!
  • @ChrisB 你已经在使用它了。 fopen($file, a) = Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it..只需使用 fwrite 并定义您要添加的新行。
  • 谢谢,@h2ooooooo,会试试看的!
  • 更好的是,不要使用 fopen/fwrite 使用更简单更快的 file_put_contents(带有 FILE_APPEND 标志),如下所示: file_put_contents( $log_filename, 'NEW LOG LINE' . PHP_EOL, FILE_APPEND ) ;
猜你喜欢
  • 1970-01-01
  • 2019-06-15
  • 2022-01-02
  • 2011-08-01
  • 1970-01-01
  • 2020-09-30
  • 2015-04-23
  • 2011-11-10
  • 1970-01-01
相关资源
最近更新 更多