Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

refreshing after php serves a download [duplicate]

Page 1 links to page 2. Page 2 serves a download using the following code:

header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/pdf');
readfile($file);
header("location: mainpage.php");

The result being the user "stays" on page 1 but is served a download.

How can I set things up, so that users remain on page 1 but it refreshes after the download is served.

I don't know javascript so I am hoping for a purely PHP solution.

like image 573
user187680 Avatar asked Oct 29 '25 16:10

user187680


1 Answers

In my opinion I wouldn't think you would necessarily need to refresh page1 at all. You should be able to force the download via a link within page1. See below:

Page1.php with a link

<a href="http://www.domain.com/page2.php?pdf=name-of-pdf">Download PDF</a>

Page2.php

$filename = $_GET['pdf'] . '.pdf';

header('Content-type: application/pdf');
header("Content-disposition: attachment; filename= '$filename'");
header("location: $filename");

This will allow the download to start whilst you remain on page1.

Hope this is what you had in mind.

like image 133
Ufb007 Avatar answered Oct 31 '25 06:10

Ufb007



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!