Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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.pdfThat'll bring up the box asking to open, save, etc.
Enjoy!

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.

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

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.

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.');

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 ?

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();
?>

![]() |
Dreamweaver Import help
|
Internet Problem: http://...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |