Periodically output to browser on long operation

老子叫甜甜 提交于 2019-12-13 16:37:55

问题


I'm using LOAD DATA INFILE to import 11M records and then the script continues with other operations. The import takes 10 minutes by which time the browser has given up (after 3 minutes) and I don't see the progress of the subsequent operations. Is there a way to output something to the browser periodically while this time consuming query is running. Phpmyadmin seems to do something like this and the browser doesn't time out.


回答1:


Check out flush() (http://php.net/manual/en/function.flush.php) or ob_flush() (http://www.php.net/manual/en/function.ob-flush.php).

Basically you generate your output (current status in this case) normally then force the server to output what is currently in the buffer.

If your operation is taking 10 minutes you will likely need to use set_time_limit($seconds) (http://php.net/manual/en/function.set-time-limit.php), be careful you don't just set it to some big amount in case the page does hang.

If you want to output status every 10% (1 minute) you could reset the timeout for 2 minutes after each update has been flushed. This way your script will timeout if it goes beyond expected time running.



来源:https://stackoverflow.com/questions/8264324/periodically-output-to-browser-on-long-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!