How to create files that Download instead of opening in the browser???
10-12-06, 07:59 PM
New Member
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
How to create files that Download instead of opening in the browser???
Hi there,
Sorry to be such a pain but I am just looking for the way to make files downloadable.
IE: I have a website with information in PDF format that I want people to download and save instead of reading in the browser window.
But clicking on the links does open the PDF inside the browser.
So all I would like is to know how to make the little "save as" box appear so they can save the PDF, instead of it automatically opening.
Thanks very much,
Nikkii The Noob
__________________
Nikkii The Noob
Goatrope - Halfway Between FUBAR and SNAFU
10-13-06, 02:38 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Does your server support PHP?
10-13-06, 03:15 AM
New Member
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hello
Yes it does. Its a Linux server with seemlingly standard stuff installed. I know it has the latest version of PHP4, and MYSQL etc.
Thanks very much for replying.
__________________
Nikkii The Noob
Goatrope - Halfway Between FUBAR and SNAFU
10-13-06, 03:25 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Give this a try. Save it for example as download.php and link to the files you want to download like this.
download.php?file=yourfile.pdf
PHP Code:
<?php
$filedir = './files/' ;
$filename = basename ( $_GET [ 'file' ]);
$path = $filedir . $filename ;
if (! is_file ( $path ) OR connection_status () != 0 )
{
exit();
}
ob_end_clean ();
header ( 'Cache-Control: no-store, no-cache, must-revalidate' );
header ( 'Cache-Control: post-check=0, pre-check=0' , false );
header ( 'Pragma: no-cache' );
header ( 'Expires: ' . gmdate ( 'D, d M Y H:i:s' , mktime ( date ( 'H' )+ 2 , date ( 'i' ), date ( 's' ), date ( 'm' ), date ( 'd' ), date ( 'Y' ))). ' GMT' );
header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ). ' GMT' );
header ( 'Content-Type: application/octet-stream' );
header ( 'Content-Length: ' . @ filesize ( $path ));
header ( 'Content-Disposition: attachment; filename="' . $filename . '"' );
header ( 'Content-Transfer-Encoding: binary' );
if ( $file = @ fopen ( $path , 'rb' ))
{
while (! feof ( $file ) AND connection_status () == 0 )
{
echo fread ( $file , 1024 * 8 );
}
flush ();
}
@ fclose ( $file );
?>
10-13-06, 06:16 AM
New Member
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Nico
Its probably just me, but can I check where the download.php file needs to go, and where download.php?file=yourfile.pdf needs to go in my HTML? Do I need to customise anything in the download.php?
Thanks very much for helping with my basic query!
__________________
Nikkii The Noob
Goatrope - Halfway Between FUBAR and SNAFU
10-13-06, 06:53 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Save the download.php file in the same directory than your HTML files. And change your links to this
Instead of linking directly to the PDF like this
If your PDFs are in the same directory than the HTML files, then change the first variable in the php file to this
10-13-06, 07:32 AM
New Member
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Ha! Its so simple now. I need to rest my brain more and stop staying up so late
A million thank yous... I just hope I can help someone out here one day
__________________
Nikkii The Noob
Goatrope - Halfway Between FUBAR and SNAFU
10-13-06, 07:39 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
No problem.
Also, be aware that all files in this directory are now downloadable. The user just has to change the URL. I would suggest creating a folder for your PDFs or other files you want to be downloadable, and change the dir name in the download.php file. If you just have HTML files in the same directory, it shouldn't be much of a risk. But if you have any PHP files which hold passwords or whatever, I'd change it.
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off