You have a few options:
Command Line Your best option is to run this on the command line manually. Command line scripts are much better for running long running processes.
Shell Exec
If you must run this via browser, you can trigger the command line script by using exec
eg
shell_exec('php -f /var/www/domain.com/myLongRunningProcess.php > /dev/null 2>/dev/null &')
Ignore User Abort
You can also run things after the browser has detached from a browser session. This is the most complex and hardest to debug but it will work. You must calculate the exact size of the page and then send the output and use the function ignore_user_abort
so that your script will continue to run when the browser disengages.