I've got a setup where a site generates internal analytics about site usage. Users are able to request a download of their personal analytics, and because this information is sensitive I do not create any files, I generate a CSV file in a variable and then "push" it to the user with this:

function _cex_download( $output, $filename ) 
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: private");
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=$filename");
  header("Accept-Ranges: bytes");
  echo $output;

This causes the user's browser to pop a "save file" dialog, and the user gets the file created on their end. This works fine, except the site's administrators are able to trigger 'destructive exports' - and in that situation I have a secondary confirmation form the admin needs to accept before the destructive export executes.

The problem is that this "push download" routine ends the Drupal page processing. There does not appear any way to redirect the admin to some page after that push download routine is called. Admins get a 'save file' dialog popped by their browser, but they remain on the confirmation form.

I tried going the multi-step form route, but after a day of dinking around with strange redirects, I backed out of that confusion. Started looking at ChaosTool's multi-step form logic too. But something tells me that the issue really is that this "push downloading" is a guided-process-killer.

Perhaps a jquery confirmation dialog that intercepts the initial 'Export' button click, do the confirmation entirely in javascript, forget out having a php/FAPI confirmation form, and when the user confirms in jquery I programmatically submit the Export I intercepted?

4 Answers 4


There seem to be some tricks, no idea how well they work inside Drupal.

See for example http://www.willmaster.com/blog/automation/one-link-download-and-redirect.php

  • Your link looks very promising. I try it later and report back. Mar 24, 2011 at 15:05

As far as I know, that is not possible. You can either send content (in your case a file), or redirect. Not both. This is a limitation of the HTTP protocol


I think your best bet might be to generate a token when the user submits the form, and then have the token store all the necessary information to generate the CSV file. Have the form redirect the user to the appropriate page, and use drupal_set_message() to provide a link that contains the token.

When the link is clicked, the CSV file can be generated and sent to the user at that time. Granted, it's more complicated than just generating it on form submit, but as Fuzzy76 says, you're up against a limitation of the HTTP protocol. Doing it this way might be simpler than trying to make a JavaScript-based solution.


This is how I Achieved it in Drupal via Jquery / JS My Answere here -> I achieved it via JS, Fine my answer here -> https://stackoverflow.com/questions/822707/php-generate-file-for-download-then-redirect/25843144#25843144

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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