php无法锁定文件

时间:2012-08-02 23:00:45

标签: php email error-handling

我的php函数似乎无法锁定它生成的警报文件,每当记录错误时都会产生大量电子邮件。

我已经检查了警报文件的权限,并且警报文件包含了所有内容,因此我不确定它为什么不起作用。

PHP脚本文件本身也启用了所有权限。

任何指针?

顺便说一下,有问题的功能如下:

function AlertAdmin($subject, $message)
{
    global $FILE;
global $ADMIN_EMAIL;
global $ALERT_SUPPRESSION_WINDOW;
$FROM_EMAIL="xxx@xxx.com";

$now = time();
    $errors = array();
    $errorID = md5($subject);
    $lastTime = 0;

    $file = @fopen($FILE, 'r+');
    if ($file === FALSE)
{
    SendEmail("Could not open: $FILE!", "The AlertAdmin function in                  /includes/errorHandler.php was unable to lock $FILE.
        Until this is fixed you may recieve multiple notifications of the same errors.<br />
        The original alert follows:<br />
        <br />
        Subject: $subject
        Message:<br />
        $message", $ADMIN_EMAIL, $FROM_EMAIL);
    return;
}
flock($file, LOCK_EX);

$lines = file($FILE);
foreach ($lines as $line)
{
    $split = explode(":", $line);
    if ($split[0] == $errorID)
    {
        $lastTime = $split[1];
    }
    $errors[$split[0]] = $split[1];
}

$errors[$errorID] = $now . "\n";

if ((time() - $lastTime) > ($ALERT_SUPPRESSION_WINDOW * 60))
    SendEmail($subject, $message, $ADMIN_EMAIL, $FROM_EMAIL);

ftruncate($file, 0);
foreach ($errors as $id => $time)
    fwrite($file, "$id:$time");
flock($file, LOCK_UN);
fclose($file);
}

$ file定义如下:

$FILE=$_SERVER["DOCUMENT_ROOT"].$WEBROOT."/filepath/alerts.log";

任何指针? 感谢。

0 个答案:

没有答案