0

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

然后我们改为使用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('some@email.com', 'TEMPLATE ERROR: '.$ResultSelectEmails[$key]['template'],$body);

    exit;

}
4

1 回答 1

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('some@email.com', 'FILE WRITING ERROR', $body);
}
$bytes = file_put_contents($serverSettings['root'].'my/tmp/emaillogs/'.$PDFLogFile.'.html', $Contents);

然后,您可以首先检查该文件是否已经存在或不可写。

于 2016-11-23T18:28:08.210 回答