Generate download file link in PHP

php

Question

Possible Duplicate:
open download dialog with php

I have a link in my page say, <a href='test.pdf'>(Test.pdf)</a>. When I click on that link, download dialogue box should open to download that file. Can anyone help me in implementing this in PHP?

thanks

1
15
5/23/2017 12:09:17 PM

Accepted Answer

$filename = 'Test.pdf'; // of course find the exact filename....        
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers 
header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));

readfile($filename);

exit;

Name the above file as download.php

HTML:

<a href="download.php">Test.pdf</a>

That should do it.

38
10/25/2012 2:52:48 AM

<a href="test.pdf">test.pdf</a>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon