0

I've got a PHP script that generates some HTML reports, which take a long time to generate (over 20-30 seconds).

Right now , the file reports.php is echoing the output to the user via the browser, saving the reports using output buffers. (EDIT: The name of the report file is a code generated by reports.php) :

<?php
   ob_start(); 

   [...echo all the html for the tables in the report...]

   file_put_contents('report.html', ob_get_contents());

   // end buffering and displaying page
   ob_end_flush(); 
 ?>

Since my process often takes a long time,I don't want the user to be stuck looking at the screen for a long time waiting for the output, or even worse, timing out.

I did a bit of searching and found out that the best way to avoid this would be to launch this process in the background using PHP CLI. I'm using a separate file:

<?php
echo 'Your report is being generated. You'll get it via email.';
exec('nohup php reports.php > /dev/null 2>&1 &');
?>

But this solution does not let me save using the output buffer. I guess it's because I'm redirecting to /dev/null?

I then tried just launching the process to the background

<?php
echo 'Your report is being generated. You'll get it via email.';
exec('nohup php reports.php');
?>

But this still won't save. What am I doing wrong? Are there any options to save this report to disk without making the user wait with the browser window open for the report to be echoed back?

RafaelM
  • 159
  • 4
  • 13

0 Answers0