How to get partial output when script is executing?

前端 未结 3 671
长情又很酷
长情又很酷 2020-12-10 17:31

Description:

I have a script which do multiple actions in longer time. In standard situation output from PHP script is sent after full execution of this script. Use

相关标签:
3条回答
  • 2020-12-10 17:49

    Yes, it should be achieveable with PHP alone. I remember seeing some code related to this when I was tracing through wordpress a while back, when it updates modules, it has to ftp, unzip, copy files, etc, which take a long time, and it likes to keep the user updated with what it's doing at the moment... after inspecting their send_message() function, and reading the PHP ob_flush() page, i think you want:

    echo "stuff\n"; // adding a newline may be important here,
                    // a lot of io routines use some variant of get_line()
    ob_end_flush(); // to get php's internal buffers out into the operating system
    flush(); // to tell the operating system to flush it's buffers to the user.
    

    The people at the php manual also implied that it might help to explicitly set a header() with the mime-type and character set in it, so the browser would know that at the start and wouldn't wait for the entire binary object to become available before attempting to decypher what sort of entity it had.

    If that doesn't work you'd need to further modify your system php.ini to turn output buffering and compression off, at which point you may as well go look at an ajax solution.

    An ajax solution would look something like:

    Your script spits out some html/javascript to hit an ajax request, flushes all it's buffers, and stops concerning itself with the user, then starts some operation, or perhaps merely indicates that some large operation is to commence in a database somewhere for a cron job to pick up. The javascript on your page would have a timer loop to poll an ajax endpoint for status until the ajax replied that it was complete. The ajax endpoint would check on the status of your task by querying the database, or examining output files, etc, and spit all it knew out at once and terminate, letting the client decide when to ask it if things were done again. It is a much more involved and complicated endevour with more moving parts, but achieves a very nice end product for the user, if it's worth your time to craft.

    0 讨论(0)
  • 2020-12-10 17:50

    Try calling in every loop ob_flush and ob_clean. The first one send the output buffer and the second one erase it.

    0 讨论(0)
  • 2020-12-10 18:15

    Call flush(); just after your code outputs "Element X finished. Y time."

    By default PHP will store all output in the output buffer until it's ready to send to the browser. flush() and ob_flush() force output to be sent.

    0 讨论(0)
提交回复
热议问题