2016-11-23 113 views
0

我有一个脚本,用于将twig/email的输出复制到一个tmp文件中,我们需要为了合法目的而存储该文件。首先这是通过使用fopen,fwrite,close完成的。这工作多年,突然它停止工作和文件。file_put_contents有时可以正常工作,并不总是

然后我们改为使用file_put_contents。但是,这会导致相同的情况。大多数文件都是创建的,但有15-25%的文件不存储在本地,而有内容。

当file_put_contents失败时,$ bytes不会输出任何内容。错误日志不显示任何内容。目前该文件夹中有324.000个文件,我们将其降低到最大10.000。

出了什么问题,或者有人可以指出我采用不同的调试方法?请参阅下面的代码。

其他信息,存储文件的文件夹具有正确的权限。脚本由cronjob执行。

try { 
    $Contents = $twig->render(stripslashes($TemplateDetails['templateHTML']), $EmailData); 
    $Subject = $twig->render(stripslashes($TemplateDetails['templateSubject']), $EmailData); 

    $bytes = file_put_contents($serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html', $Contents); 

    /* 
    $WriteFile = fopen($serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html','w'); 
    $bytes = fwrite($WriteFile, $Contents); 
    fclose($WriteFile); 
    */ 

    echo $Output->getColoredString('Wrote '.$bytes.' to file '. $serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html')."\n"; 

    // Create the Mailer using your created Transport 
    $mailer = Swift_Mailer::newInstance($transport); 

    $logger = new \Swift_Plugins_Loggers_ArrayLogger(); 
    $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger)); 

    $message = Swift_Message::newInstance($Subject) 
     ->setFrom(array($ResultSelectEmails[$key]['fromEmail'] => $ResultSelectEmails[$key]['fromName'])) 
     ->setTo(array($ResultSelectEmails[$key]['toEmail'])) 
     ->setBody($Contents, 'text/html', 'UTF-8') 
    ; 

    if (!$mailer->send($message, $errors)) { 
     // Dump the log contents 
     // NOTE: The EchoLogger dumps in realtime so dump() does nothing for it. We use ArrayLogger instead. 
     echo $Output->getColoredString(" - [ERROR] " . $logger->dump(), "red") . "\n"; 
    }else{ 
     echo $Output->getColoredString('- [SEND] '.date('Y-m-d H:i:s'), 'green') . "\n"; 
    } 


}catch (\Exception $exc) { 

    $body = "TemplateId: ".$ResultSelectEmails[$key]['template']."\n"; 
    $body .= "ShopId: ".$ResultSelectEmails[$key]['shopId']."\n"; 
    $body .= "--------------------------------------------------------\n"; 
    $body .= "String Error: ". $exc->getTraceAsString()."\n"; 
    $body .= "Line: ".$exc->getLine()."\n"; 
    $body .= "File: ".$exc->getFile()."\n"; 

    mail('[email protected]', 'TEMPLATE ERROR: '.$ResultSelectEmails[$key]['template'],$body); 

    exit; 

} 
+0

'$ PDFLogFile'中有什么?另外,我会尝试抛出一个异常,当'$ bytes == false'并且试图知道这个问题发生在哪个文件中时。 –

+0

$ PDFLogFile是电子邮件的标识加上交易散列。例如“1345234-sadfafgasdfasgdsfhggaghf”。那些始终存在并且总是独一无二的。 –

+0

如果你正在用你的fopen群,那么你正在等待最后一个用户完成。 file_get_contents不会等待,它只是说,抱歉,有另一个用户在那里写,稍后尝试;在这种情况下,您的代码必须稍后尝试。尝试将数据放入某个临时存储中,然后使用预定作业将它们从临时队列中弹出。 – WEBjuju

回答

1

就在

$bytes = file_put_contents($serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html', $Contents); 

把这个代码 - 它会帮助你确定这是怎么回事:

if (is_writable($serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html')) { 
    // no worries, just keep trucking 
    // echo 'The file is writable'; 
} else { 
    $body = "Error writing file: ".$serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html'."\n"; 
    $body .= "Date Time: ".date('Y-m-d H:i:s')."\n"; 
    $body .= "--------------------------------------------------------\n"; 
    $body .= "Data to have been written: ".$Contents."\n"; 

    mail('[email protected]', 'FILE WRITING ERROR', $body); 
} 
$bytes = file_put_contents($serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html', $Contents); 

然后你可以通过查看该文件是否已经存在或已开始不可写入的状态。

+0

我已经实现了你的脚本。有些电子邮件已收到FILE WRITING ERROR。奇怪的是,这些文件已被写入。 –

+0

“拖尾”文件给我结果。这是否意味着由于磁盘繁忙的原因而无法写入文件? –

+0

啊,file_put_contents shoudl在is_writable块内 - 我的错误。仍然有趣的是,在这个微秒的范围内,它从不可写入到可写。它可能是任何会使文件无法写入,我想。为什么不回到fopen如果这是完美的工作? – WEBjuju

相关问题