Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate download file link in PHP [duplicate]

Tags:

php

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

like image 785
Thinker Avatar asked Dec 28 '09 05:12

Thinker


2 Answers

$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.

like image 104
Jacob Relkin Avatar answered Nov 27 '22 18:11

Jacob Relkin


<a href="test.pdf">test.pdf</a>
like image 30
Galen Avatar answered Nov 27 '22 18:11

Galen