Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP generate file for download then redirect

I have a PHP app that creates a CSV file which is forced to download using headers. Here's the relevant part of the code:

header('Content-Type: application/csv'); 
header("Content-length: " . filesize($NewFile)); 
header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
echo $content;
exit(); 

What I'd like to do is redirect users to a new page after the file is built and the download prompt is sent. Just adding header("Location: /newpage") to the end didn't work, expectedly, so I'm not sure how to rig this up.

like image 877
Evan Avatar asked May 05 '09 00:05

Evan


5 Answers

I don't think this can be done - although I am not 100% sure.

The common thing (e.g. in popular download sites) is the reverse: first you go to the after page and then the download starts.

So redirect your users to the final page that (among other things) says:

Your download should start automatically. If not click [a href="create_csv.php"]here[/a].

As about initiating the download (e.g. automatically calling create_csv.php) you have many options:

  • HTML: [meta http-equiv="refresh" content="5;url=http://site/create_csv.php"]
  • Javascript: location.href = 'http://site/create_csv.php';
  • iframe: [iframe src="create_csv.php"][/iframe]
like image 149
daremon Avatar answered Nov 17 '22 08:11

daremon


very easy to do in the case it is really needed.

But you will need to have a bit work in JavaScript and cookies:

in PHP you should add setting up a cookie

header('Set-Cookie: fileLoading=true'); 

then on the page where you call the download you should track with JS (e.g. once per second) if there is coming cookie like that (there is used plugin jQuery cookie here):

setInterval(function(){
  if ($.cookie("fileLoading")) {
    // clean the cookie for future downoads
    $.removeCookie("fileLoading");

    //redirect
    location.href = "/newpage";
  }
},1000);

Now if the file starts to be downoaded JS recognizes it and redirects to the page needed after cookie is deleted.

Of course, you can tell you need browser to accept cookies, JavaScript and so on, but it works.

like image 45
smnbbrv Avatar answered Nov 17 '22 10:11

smnbbrv


The header you are sending are HTTP headers. The browser takes that as a page request and processes it as a page. And in your case, a page it needs to download.

So adding a redirect header to that confuses the whole process of downloading the file (since headers are collected, generated into one header and then sent to the browser, you can try this by setting multiple redirect headers IIRC)

like image 10
Ólafur Waage Avatar answered Nov 17 '22 08:11

Ólafur Waage


This is quite old issue, but here is how I achieved it via JS.

// Capture the "click" event of the link.
var link = document.getElementById("the-link");
link.addEventListener("click", function(evt) {
  // Stop the link from doing what it would normally do.
  evt.preventDefault();
  // Open the file download in a new window. (It should just
  // show a normal file dialog)
  window.open(this.href, "_blank");
  // Then redirect the page you are on to whatever page you
  // want shown once the download has been triggered.
  window.location = "/thank_you.html";
}, true);

Via - https://www.daniweb.com/web-development/php/threads/463652/page-not-redirecting-after-sending-headers-in-php

like image 4
Vaibhav Jain Avatar answered Nov 17 '22 08:11

Vaibhav Jain


Bear in mind, however, the automatic initiation of downloadable files for IE users will trigger the security warning tab. All three of the methods outlined by daremon would show this warning. You simply can't get around this. You will be better served if you provide real links.

like image 3
martar Avatar answered Nov 17 '22 08:11

martar