简体   繁体   English

file_put_contents有时有效,但不总是有效

[英]file_put_contents sometimes works, not always

I have a script that copies the output of twig / email to a tmp file that we need to store for legal purposes. 我有一个脚本,可将树枝/电子邮件的输出复制到我们出于法律目的需要存储的tmp文件中。 Firstly this was done by using fopen, fwrite, close. 首先,这是通过使用fopen,fwrite,close完成的。 That worked for years, all of sudden it stopped working and the files. 那工作了好多年,突然间所有文件都停止工作了。

Then we changed to using file_put_contents. 然后我们改为使用file_put_contents。 However that causes the same situation. 但是,这会导致相同的情况。 Fast majority the files are created, however 15-25% the files are not store locally while there is content. 创建文件的速度最快,但是有内容时,文件的15-25%不会存储在本地。

$bytes doesn't output anything when the file_put_contents fails. 当file_put_contents失败时,$ bytes不输出任何内容。 The error logs don't show anything. 错误日志未显示任何内容。 Currently there are 324.000 files in the folder, we are bringing that down to 10.000 max. 当前文件夹中有324.000个文件,我们将其降低到最大10.000。

What is going wrong, or can somebody point me to a different debug approach? 发生了什么问题,或者有人可以将我引向其他调试方法? See the code below. 请参见下面的代码。

Additional info, folder where the files are stored has correct permissions. 其他信息,文件所在的文件夹具有正确的权限。 Script is executed by an cronjob. 脚本由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;

}

Just before 就在之前

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

put this code - it will help you determine what's up: 输入以下代码-它可以帮助您确定发生了什么:

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);

Then you can begin by checking to see if that file exists already or is unwritable. 然后,您可以先检查该文件是否已经存在或不可写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM