When a script is taking a while, the server (Apache) tries to send it as a download. It does not have this problem when the time-intensive loop in the file is made shorter, implying that it's simply the length of script execution that is causing the problem.
I have timeout set to infinite in PHP, so why would it be sending as a download instead of running?