Current location: Hot Scripts Forums » General Community » Script Requests » How to create files that Download instead of opening in the browser???


How to create files that Download instead of opening in the browser???

Reply
  #1 (permalink)  
Old 10-12-06, 07:59 PM
forbiddenpassionfruit's Avatar
forbiddenpassionfruit forbiddenpassionfruit is offline
New Member
 
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question 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
Reply With Quote
  #2 (permalink)  
Old 10-13-06, 02:38 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Does your server support PHP?
Reply With Quote
  #3 (permalink)  
Old 10-13-06, 03:15 AM
forbiddenpassionfruit's Avatar
forbiddenpassionfruit forbiddenpassionfruit is offline
New Member
 
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Cool

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
Reply With Quote
  #4 (permalink)  
Old 10-13-06, 03:25 AM
Nico's Avatar
Nico Nico is offline
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')+2date('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($file1024 8);
    }

    
flush();
}

@
fclose($file);

?>
Reply With Quote
  #5 (permalink)  
Old 10-13-06, 06:16 AM
forbiddenpassionfruit's Avatar
forbiddenpassionfruit forbiddenpassionfruit is offline
New Member
 
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

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
Reply With Quote
  #6 (permalink)  
Old 10-13-06, 06:53 AM
Nico's Avatar
Nico Nico is offline
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

Code:
<a href="download.php?file=yourfile.pdf">Download</a>
Instead of linking directly to the PDF like this
Code:
<a href="yourfile.pdf">Download</a>
If your PDFs are in the same directory than the HTML files, then change the first variable in the php file to this
PHP Code:

$filedir './'
Reply With Quote
  #7 (permalink)  
Old 10-13-06, 07:32 AM
forbiddenpassionfruit's Avatar
forbiddenpassionfruit forbiddenpassionfruit is offline
New Member
 
Join Date: Oct 2006
Location: Brisbane, Australia
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb

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
Reply With Quote
  #8 (permalink)  
Old 10-13-06, 07:39 AM
Nico's Avatar
Nico Nico is offline
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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fastest upload and download bulky files like images,movie,video etc. aaryan_wind PHP 2 07-05-10 08:39 AM
create files in php banli PHP 3 10-31-08 09:49 PM
stopping direct download of my pdf files .. markmarkymark Script Requests 5 01-23-04 01:35 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
download files sasi ASP 0 09-10-03 08:29 PM


All times are GMT -5. The time now is 11:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.