Computing.Net > Forums > Web Development > download pdf hyperlink

download pdf hyperlink

Reply to Message Icon

Original Message
Name: firefox86
Date: August 14, 2004 at 03:10:22 Pacific
Subject: download pdf hyperlink
OS: Windows XP
CPU/Ram: 1.80Ghz, 256RAM
Comment:

Hi,
I have a file that is in .pdf format for my website.

If you left click on the link, it will open the Adobe Reader and open the file.

What I want to do is when you left click, it opens a dialogue box that asks you where you want to save the file.

I know you can do this by right clicking and choosing save as, but is there any way by just one simple left click to downlaod it?

Thanks

Mike


Report Offensive Message For Removal


Response Number 1
Name: Jamie_McCoy
Date: August 14, 2004 at 07:02:19 Pacific
Subject: download pdf hyperlink
Reply: (edit)

you could put it in a zip folder, that would work

Jaymc.co.nr


Report Offensive Follow Up For Removal

Response Number 2
Name: FBI Agent
Date: August 14, 2004 at 09:53:35 Pacific
Subject: download pdf hyperlink
Reply: (edit)

im pretty sure there is a way, i just dont know what it is. i saw it a while ago when i was practicing php and looking at www.php.com. it has little tutorials and stuff and it just happened to show a small snippet of what you need to do that.

FBI Agent

AIM: EliteAssassin187


Report Offensive Follow Up For Removal

Response Number 3
Name: FBI Agent
Date: August 14, 2004 at 09:55:37 Pacific
Subject: download pdf hyperlink
Reply: (edit)

crap, that was php.net lol

FBI Agent

AIM: EliteAssassin187


Report Offensive Follow Up For Removal

Response Number 4
Name: -Bryan-
Date: August 14, 2004 at 16:08:11 Pacific
Subject: download pdf hyperlink
Reply: (edit)

Create a file called dowload.php containing the following:

<?php
$filename = $_GET['filename'];
if( ! is_file($filename) || $filename[0] == '.' || $filename[0] == '/' )
die("Bad access attempt.\n");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

When you want to call the file use:
http://www.yourwebsite.com/download.php?filename=acrobatfilename.pdf

That'll bring up the box asking to open, save, etc.

Enjoy!


Report Offensive Follow Up For Removal

Response Number 5
Name: -Bryan-
Date: August 14, 2004 at 16:11:31 Pacific
Subject: download pdf hyperlink
Reply: (edit)

And forgive my ridiculous spelling errors. =)

The filename at the top should read download.php not dowload.php

Of course, you could name the file whatever you like. That won't have any effect on the script.


Report Offensive Follow Up For Removal


Response Number 6
Name: Khalid
Date: August 14, 2004 at 23:13:03 Pacific
Subject: download pdf hyperlink
Reply: (edit)

This is a nice way of forcing downloads, but I think with this script people can access all your other scripts aswell; that doesnt seem like a very secure thing...
Perhaps an extra if-then - check is needed to prevent the download-script from sending files you dont want to share...


Report Offensive Follow Up For Removal

Response Number 7
Name: -Bryan-
Date: August 15, 2004 at 00:00:57 Pacific
Subject: download pdf hyperlink
Reply: (edit)

Hmmmmmm. Good point. Now that I look at it, you're right. And I could have sworn I fixed that. Argh.

I guess you could always hardcode the filename into the php file, i.e. $filename = 'file.pdf' where file.pdf is the actual file name.

Hmmmmm. Let me go back to working on this.


Report Offensive Follow Up For Removal

Response Number 8
Name: -Bryan-
Date: August 15, 2004 at 14:48:59 Pacific
Subject: download pdf hyperlink
Reply: (edit)

Okay, you could slap this in to limit the file type to pdf or whatever:

$file = explode('.', $filename);
if($file[1] != 'pdf')
die('Could not download selected file.');

or I suppose you could make it so it won't download php scripts:

$file = explode('.', $filename);
if($file[1] = 'php')
die('Could not download selected file.');



Report Offensive Follow Up For Removal

Response Number 9
Name: firefox86
Date: August 16, 2004 at 05:03:01 Pacific
Subject: download pdf hyperlink
Reply: (edit)

wow.. uumm headache :P i'm a newbie at php scripting lol

i can only do simple email scripts where you fill out a form and click send.

So all I need to do is copy the first script that you sent, and add the last bit that you sent to it.. and save it as .php ?


Report Offensive Follow Up For Removal

Response Number 10
Name: firefox86
Date: August 16, 2004 at 05:10:58 Pacific
Subject: download pdf hyperlink
Reply: (edit)

aahh yes it worked.. thank you so much!! very very helpful :)


Report Offensive Follow Up For Removal

Response Number 11
Name: -Bryan-
Date: August 16, 2004 at 13:04:37 Pacific
Subject: download pdf hyperlink
Reply: (edit)

Woo! Good to hear it worked!! And correct, the final script would look like this...just be sure to change where it says pdf to whatever file type you are using:


<?php
$filename = $_GET['filename'];
if( ! is_file($filename) || $filename[0] == '.' || $filename[0] == '/' )
die("Bad access attempt.\n");
$file = explode('.', $filename);
if($file[1] != 'pdf')
die('Could not download selected file.');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: download pdf hyperlink

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software