What you can do is this:

Make a folder on your server, write the file to that folder. When the script is done running, output a link to the dir/filename for the user to click on and download that file.

I did this exact thing at work a few weeks ago. If you need an example of how it's done, email me: [email protected]