Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:44:03+00:00 2026-05-26T21:44:03+00:00

I have a PHP script that takes a few minutes to finish processing. While

  • 0

I have a PHP script that takes a few minutes to finish processing. While the page is still loading, I want to show part of the PHP output as it becomes available, which can be done using ob_start() and ob_flush().

After the entire script has finish executing, I want to save all the PHP output right from the start into a HTML file. This can be done using ob_start() and file_put_contents("log.html", ob_get_contents());

Problem: However, because we are calling ob_flush() along the way, the final file that gets saved with file_put_contents() appears to be separated into different files. I suspect this has to do with the buffer being cleared by the ob_start() calls before file_put_contents() is called, but why did it not just save the output between the final ob_flush() and file_put_contents() to the file, but instead saves several different files? (I may be wrong, the seperate partial files may be due to partial execution of the script)

In other words, how do I show PHP output as a long script executes, and still save all the PHP output to a single HTML file?

PHP Code

// Start the buffering
ob_start();

......

ob_flush();

......

ob_flush();

......

file_put_contents("log.html", ob_get_contents());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:44:03+00:00Added an answer on May 26, 2026 at 9:44 pm

    Couple of ways I can think of:

    1. Keep a variable (called something like $content), and append the current buffer every time you call ob_flush():

      $content = '';
      ...
      $content .= ob_get_contents();
      ob_flush();
      ...
      $content .= ob_get_contents();
      ob_flush();
      ...
      file_put_contents('log.html', $content . ob_get_contents());
      ob_flush();
      
    2. Use fopen():

      $fp = fopen('log.html', 'w+');
      ...
      fwrite($fp, ob_get_contents());
      ob_flush();
      ...
      fwrite($fp, ob_get_contents());
      ob_flush();
      ...
      fwrite($fp, ob_get_contents());
      fclose($fp);
      ob_flush();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that takes 2 or 3 minutes to complete. I
I have a PHP script that takes a long time (5-30 minutes) to complete.
I have a php pipe script that receives an email, takes an attached *.csv
I have a php script that sends SMS, the problem is that it takes
I have a php script that can take anything from 2 to 10 minutes
I have a php script that takes the individual characters entered into a form
I have a simple PHP mailer script that takes values from a form submitted
I have a php script that takes hierarchical data from a mysql procedure and
I have a jQuery script that I am including on a PHP page that
So, I have a PHP script that takes a users 'key' and checks if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.