繁体   English   中英

允许php写入日志文件

[英]allow php to write log files

我有一个想要创建日志文件的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 );
?>

快速解答:将写入权限添加到您要写入日志的目录:

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

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

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

导航到日志目录,然后尝试sudo chmod -R 777 .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM