Image link that downloads a pdf
Posted: Fri Jun 05, 2009 11:57 am
Hi I want to have a image link where people can click to download a pdf file. I would appreciate any sort of help.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>Well, here you have to make yourself familiar with HTTP, not with PHP, because header() function sends HTTP headers. However, I assume you are looking for quick solution (read about the HTTP protocol as soon as you can), so I will tell you, that you have to send a Content-type header (usualy application/ocstream to force download instead of opening with the configured plugin; this is like saying "unknown type, so you cannot open it right now, you have to downloading first) and Content-disposition: attachment. I'm writing this on prima vista, I didn't check the exact spelling, but I believe that you can find this with one search in Google. So:thoque wrote:don't understand how to use that.
Code: Select all
header('Content-type: application/ocstream');
header('Content-Disposition: attachment; filename="your-pdf-file.pdf"');
readfile('your-pdf-file.pdf');It's all about the browser. I do not remember exactly, as this was 2 or 3 years ago when I had problems with downloading the file... as far as I remember some of the major browsers was ignoring the content-disposition header. Just try your code with IE 6, FF and Opera... If it works with all of them, than I'm just wrong. But I have the habit to use octet-stream content-type since the time I was implementing the downloading of files for the first timemikemike wrote:Why not use the proper mime-type like the php.net documentation does? I'm not trying to sound coy, I'm genuinely intrigued
Somewhere at Internet... wrote:To be on the safe side and ensure consistent behavior in all browsers, it's usually better to use both:
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"My Text File.txt\"