Vinoth
Vinoth

Reputation: 1349

Header redirect after download

I am new to PHP and learning it step by step. Well I've a download page that I want to be redirected after the download has been completed.

header('Content-type: application/octet-stream');                 
header('Content-Disposition: attachment; filename="dlink.pdf"');                  
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");                  readfile('dlink.pdf');   
header("refresh: 2; auto_works.html");
exit;

This is my download code enclosed within the PHP tag. How can I achieve my directive ? Please help. Could you also mention to me where am I supposed to write the line of code

Thanks

Upvotes: 0

Views: 5154

Answers (3)

Ondřej Mirtes
Ondřej Mirtes

Reputation: 5616

This is not possible with pure HTTP. Either you respond to a user request with a download response or a redirect response.

Of course you can do some client-side scripting and do two things at once - e.g. open a pop-up window leading to file download and load a new page in the original window at the same time. But I discourage you to do this, because it's not user-friendly and might not work with some configurations at all.

Upvotes: 0

Rudie
Rudie

Reputation: 53781

You can't redirect after the download is complete. The webserver (which would do the redirect) doesn't know when the download is complete. There are ofcourse ways, but they're difficult and not worth it.

You 'should' redirect after the download started.

Upvotes: 1

user744116
user744116

Reputation:

Try to set

header("Location: auto_works.html");exit; 

instead of

header("refresh: 2; auto_works.html"); exit;

Upvotes: 0

Related Questions