【发布时间】:2011-06-11 03:04:24
【问题描述】:
我正在尝试创建一个简单的错误日志来记录 php 错误,以防无法将这些错误报告给管理员(通过数据库连接)。我是这样写的:
function errorLog($array)
{
$time = date("F j, Y, g:i a");
$ip = $_SERVER['REMOTE_ADDR'];
$no = $array['e-no'];
$string = $array['e-string'];
$file = $array['e-file'];
$line = $array['e-line'];
$context = $array['e-context'];
$string = "TIME {$time} IP {$ip} ERROR {$no} | {$string} | {$file} | {$line} | {$context}";
if ($handler = fopen('log/log-file.txt', 'a'))
{
fwrite($handler, $string);
fclose($handler);
}
else
{
exit('Fatal Error #0002');
}
}
开始编辑
函数变成了
function errorLog($array)
{
$time = date("F j, Y, g:i a");
$ip = $_SERVER['REMOTE_ADDR'];
$type = $array['e-type'];
$string = $array['e-string'];
$file = $array['e-file'];
$line = $array['e-line'];
$context = $array['e-context']
$string = "ON {$time} IP {$ip} ERROR {$type} | {$string} | {$file} | {$line} | {$context}";
if (!error_log($string, 3, 'log/log-file.txt')) { exit('Fatal Error #0002'); }
}
结束编辑
但是当我运行它时,我得到了
Warning: fopen(log/log-file.txt) [function.fopen]: failed to open stream: Permission denied in myfile.php on line 17
我知道这是一个服务器权限问题,但我正在使用本地主机(Mac os x 上的 Xampp),我想管理这些权限。我怎样才能将 log/ 放置为仅可写?有没有更好的方法来做我想做的事情?
【问题讨论】:
-
不是您当前问题的解决方案,但您可能需要阅读函数 error_log。这可能会简化您的代码吗?
-
由您的 ftp 客户端创建该日志文件并为其设置 666 权限
-
不能用绝对路径,用apache有权限写的文件夹吗?
-
确保执行脚本的用户有写入错误日志文件的权限。如果您从浏览器运行它,那么 Apache(nobody 或 www-data)将是具有有限写入权限的用户。如果您从命令行或 CRON 运行它,请仔细检查文件权限以及文件路径,因为每个执行脚本的用户的主路径都会更改。
-
@Col。弹片,回答这个问题,你会得到接受的答案,因为你的建议解决了我的问题。
标签: php error-handling logging