It may, of course, be that there is something wrong with your code but if, as you imply, this is a timing-out issue then the canonical solution is to serve a `please wait' script with a refresh instruction in the header while your file is being written. The intermediary script checks the output progress and serves a download link on completion.
This technique has been explained with example code by merlyn in one of his famous Web Techniques columns.
In reply to Re: incomplete cgi output
by matthewb
in thread incomplete cgi output
by rvosa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |