1

I've got a form that gathers some data. The data is then modified and should be passed to the user as a file. To manage this I did the following:

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: text/out');
header('Content-Disposition: attachment; filename="file.out"');
echo $file;
module_invoke_all('exit');
exit();

What I need now is to somehow display some additional info to the user without losing the input data. In other words, when user submits a form, it should let him download the file and then, for example, print out a success message, contents of the file downloaded or another form. Is there any way to do that?

0

You need to isolate that code out of your form submission handler. When you set the Content-Disposition: attachment header it's going to override the browser behavior of loading a new page.

Instead, you should have your form handler save the output file & generate a success page containing a download link (in case your user doesn't have JS support) and run a JS setTimeout() function that will trigger a download after X seconds. See this SO post for an example.

The download link path can be generated with hook_menu(). Its callback function will use your code snippet above. Depending on the content of the download (e.g. sensitive personal data), you may need to protect this path. This can be done in a variety of ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.