469,313 Members | 2,536 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,313 developers. It's quick & easy.

Looking for a script to download pdf file from server

Hi,

I am looking for a script that will download a pdf file from my server without
opening it in IE. I want peeople to be able to simply download the file and
store it in a location on their PC. Any help greatly appreciated.

Regards
Dynamo

May 23 '06 #1
11 7951
Rik
Dynamo wrote:
Hi,

I am looking for a script that will download a pdf file from my
server without opening it in IE. I want peeople to be able to simply
download the file and store it in a location on their PC. Any help
greatly appreciated.


header('Content-disposition: attachment');

Grtz,
--
Rik Wasmus
May 23 '06 #2
Many thanks but somehow I dont think that snippet of code on its own will do
what I want. I was looking for the complete code. I have tried the following but
it failed miserably. What am I doing wrong?

<?php
$dir_name = "/books/";
$file = "downloadtest.pdf";

$content = file_get_contents($dir_name . $file);

header('Content-Type: pdf');
header('Content-Length: ' . filesize($dir_name . $file));
header('Content-Disposition: 'attachment');
echo $content;
?>

Regards
Dynamo
In article <e4**********@netlx020.civ.utwente.nl>, Rik says...

Dynamo wrote:
Hi,

I am looking for a script that will download a pdf file from my
server without opening it in IE. I want peeople to be able to simply
download the file and store it in a location on their PC. Any help
greatly appreciated.


header('Content-disposition: attachment');

Grtz,


May 23 '06 #3
Rik
Dynamo wrote:
Many thanks but somehow I dont think that snippet of code on its own
will do what I want. I was looking for the complete code. I have
tried the following but it failed miserably. What am I doing wrong?

<?php
$dir_name = "/books/";
$file = "downloadtest.pdf";

$content = file_get_contents($dir_name . $file);

header('Content-Type: pdf');
header('Content-Length: ' . filesize($dir_name . $file));
header('Content-Disposition: 'attachment');
echo $content;

1. Turn on error reporting, if this is you script, it will tell you some
interesting things.
2. Is /books/ in your root? If not, you can't refer to it like this. Does
content indeed hold the file, or just 'false"?

Also:
it's header('Content-type: application/pdf');
The exact syntax is in the php-manual as an example for header...
http://nl3.php.net/en/header

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="name_give_to_user.pdf"');
readfile('/path/to/original.pdf');

Grtz,
--
Rik Wasmus
May 23 '06 #4
Why not just add a note on screen saying something like "User of IE
right-click and then hit 'Save Target As..' to download without opening"

May 23 '06 #5
Good point. I guess it's a matter of personal preference. IMHO I don't think
that looks very professional on a company site. Sure OK for a personal site to
download music etc etc but I don't see many company sites doing that. They
mostly have a "Download here" type of button.

Kind Regards
Dynamo

In article <11**********************@j55g2000cwa.googlegroups .com>, strawberry
says...

Why not just add a note on screen saying something like "User of IE
right-click and then hit 'Save Target As..' to download without opening"


May 23 '06 #6
> Good point. I guess it's a matter of personal preference. IMHO I don't think
that looks very professional on a company site. Sure OK for a personal site to
download music etc etc but I don't see many company sites doing that. They
mostly have a "Download here" type of button.


I would say IT IS more professional to give a "right click to
download"-hint as it it showing the user how to do it right. However a
simple link is more convenient and supports unexperienced users. I
implemented that once with fpdf (fpdf.org). Its method Output() has a
parameter that exactly covers the task (more on
http://www.fpdf.org/en/doc/output.htm). I also remember that I read the
implementation and believe to remember that they worked with some dummy
Content-Disposition. You can either do that as well or create a simple
instance of fpdf and look for the header it sends with with one or the
other option. Try telnet or HTTP Live Headers (Firefox Extension) to see
the headers the server sends.
May 23 '06 #7
Dynamo:
[strawberry:]
Why not just add a note on screen saying something like "User of IE
right-click and then hit 'Save Target As..' to download without opening"


Good point. I guess it's a matter of personal preference. IMHO I don't think
that looks very professional on a company site. Sure OK for a personal site to
download music etc etc but I don't see many company sites doing that. They
mostly have a "Download here" type of button.


If you want to copy a particular website, copy it. Look at what it
says to users, look at what it says to user-agents, send requests, look
at responses.

While strawberry's suggestion would violate the general principle
that says Don't Mention the Mechanics, I regard it as less harmful than
the server-side jiggery-pokery proposed upthread.

--
Jock

May 23 '06 #8
Oops, what have I started...?

May 23 '06 #9
Many thanks. That does exactly what I want. I already looked at that page in the
php manual and the example code is quite a way down the page. Guess I must have
passed it by.

Regards
Dynamo

In article <e4**********@netlx020.civ.utwente.nl>, Rik says...
1. Turn on error reporting, if this is you script, it will tell you some
interesting things.
2. Is /books/ in your root? If not, you can't refer to it like this. Does
content indeed hold the file, or just 'false"?

Also:
it's header('Content-type: application/pdf');
The exact syntax is in the php-manual as an example for header...
http://nl3.php.net/en/header

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="name_give_to_user.pdf"');
readfile('/path/to/original.pdf');

Grtz,


May 24 '06 #10
NC
Dynamo wrote:

I am looking for a script that will download a pdf file from my server
without opening it in IE.


There is no guarantee that this will ever happen. IE ignores
"Content-Dispositoin:" headers completely.

Cheers,
NC

May 24 '06 #11
NC:
IE ignores "Content-Dispositoin:" headers completely.


Given a choice between always ignoring it or always following it, I
think always ignoring it is preferable, not only because it isn't part
of HTTP but also because always following it would grant undue power to
the publisher. But I'd like to think that a user-agent could treat
Content-Disposition as it was intended, as nothing more than a
suggestion. Nothing stops a user-agent from making the publisher's
'desired presentation' - be it the same as or different to the
user-agent's normal mode of presentation - *known* to the user, without
making it obligatory.

</rant>

--
Jock

May 24 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by FLOTServer | last post: by
28 posts views Thread by Peter Michaux | last post: by
1 post views Thread by Joey Nolan | last post: by
2 posts views Thread by Raffi | last post: by
rajiv07
4 posts views Thread by rajiv07 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.