php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80514 Output Buffer Fails on Second Run
Submitted: 2020-12-14 15:45 UTC Modified: 2020-12-14 15:49 UTC
From: folarin at engineer dot com Assigned: cmb (profile)
Status: Not a bug Package: Apache2 related
PHP Version: 7.3.25 OS: Ubuntu 20.04.1 LTS
Private report: No CVE-ID: None
 [2020-12-14 15:45 UTC] folarin at engineer dot com
Description:
------------
Hi all,

I'm using the output buffer to render a php file, capturing the content and sending it via email.

The process works fine for only one such cycle. That is only one file to read, and send as email to a user.

I usually have to send emails to user(confirming their actions) and to admin(to act on user actions) on the same http user request. The issue with the above is that output buffer returns an empty string for the second email file content. As a check, I did each email sending operation in isolation and they both worked - showing there was no errors with the individual files.

Output buffer successfully parses the first file, whatever that is, and returns the html string content. But on the second call to the method to parse using output buffer it returns an empty string.


php --version
PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

To the best of my knowledge, toggling each of the PHP_OUTPUT_HANDLER_CLEANABLE, etc options didn't help.

Thanks


Test script:
---------------
public function getEmailBody(string $php_file_template_path){
   ob_start(null,null,PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE);
   require_once($php_file_template_path);
   $xmail = ob_get_clean();
   ob_end_clean();
   return $xmail;
}

$msg = getEmailBody('path/to/file.php')
mail("user@example.com","My subject",$msg);

//this works for the above, but the following fails, that is $msg='', and so empty email content is reported:


$msg2 = getEmailBody('path/to/another/file.php')
mail("admin@example.com","My subject",$msg);

Expected result:
----------------
That both user@example and admin@example get the appropriate emails.
But in the above case, only user@example gets the email, the admin email returns an empty file read and hence empty email body


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-14 15:49 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-12-14 15:49 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Replace

    require_once($php_file_template_path);

with

    require($php_file_template_path);
 [2020-12-14 21:29 UTC] folarin at engineer dot com
You're an angel. It works... This issue has persisted for months across multiple projects. I'm grateful
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 05:01:28 2024 UTC