file_put_contents有时会起作用,而不是总是如此

时间:2016-11-23 18:00:35

标签: php file-put-contents

我有一个脚本将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;

}

1 个答案:

答案 0 :(得分: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);

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

相关问题