【问题标题】:allow php to write log files允许php写入日志文件
【发布时间】:2014-05-07 11:17:57
【问题描述】:

我有一个要用来创建日志文件的 php 脚本。但它不会这样做,权限被拒绝。 从语法上讲,我认为我的代码对于编写文件是正确的,但我不知道如何(安全地) 允许 php 在目录中创建文件。

> [Wed May 07 07:01:28.827861 2014] [:error] [pid 2457] [client
> 10.0.0.123:63383] PHP Warning:  fopen(Log filename: 1399460488.txt): failed to open stream: Permission denied in /home/j0h/www/test/iv.php on line 7, referer:
> http://10.0.0.123/test/

这是一个使用 php5.4 的 linux 服务器 这是我试图用来编写文件的代码: 第 7 行以 $handle 开头 ...

<?php
//Time... for some logs
$t=time();
//create Log filename based of Epoch
$LogName =  $t . ".txt";
$handle = fopen($LogName, 'w') or die('Cannot open file:  '.$LogName); 

$file = fopen( $LogName, "w" );
if( $file == false )
{
   echo ( "Error in opening new file" );
   exit();
}
fwrite( $file, "This is  a simple test\n" );
fclose( $file );
?>

【问题讨论】:

  • 文件是只读的吗?
  • 我想即时创建日志文件。它们还不存在。
  • 您需要在属性中授予 iv.php 文件 777 权限 - 用于读写文件。 movabletype.org/documentation/assets/777.png
  • 那么777权限呢?

标签: php linux file-io permissions


【解决方案1】:

快速回答:将写入权限添加到您正在写入日志的目录:

chmod -R +w /home/j0h/www/test/

但是,为了更安全,您应该将日志写入特定的日志目录,其中不包含任何 php 或可执行文件。

【讨论】:

  • 是的,我只是打断了我的一个想法,然后卡住了。但是当我用这个工具完成时,我当然会接受你的建议。
【解决方案2】:

这可能不是 php 问题。它是阻止写入访问的文件系统。有几种解决方案:授予目录 777 权限,让您的网络用户拥有该目录或让它加入有权限的组,...

导航到日志目录并尝试sudo chmod -R 777 .

【讨论】:

  • chmod 777 test/ 就足够了。谢谢大家!
猜你喜欢
  • 2012-09-05
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2017-05-07
  • 2014-01-05
  • 2023-03-31
相关资源
最近更新 更多