Download a file from webserver to local computer using browser

Rakish's Avatar, Join Date: Jul 2006
Go4Expert Member
Hi all,
Thanks for sharing your knowledge in the past,

I want to allow user to download a file on the webserver to their local system using Just a browser.

the file readme.txt is stored in /tmp folder.

please tell me how can i implement it.

Thanks yall,

shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Just putting that in the web server directory will allow it to download or you can use any server side script even to achieve the same. in PHP it would be something like
Code: PHP
header("Content-Length: $size");
header("Content-Type: $type");
header('Cache-Control: no-cache, must-revalidate');           // HTTP/1.1
header('Pragma: no-cache');                                   // HTTP/1.0
header('Expires: Mon, 26 Dec 1979 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-transfer-encoding: binary');

    header("Content-Disposition: attachment; filename=\"$name\"");
else if(stristr($type,"image"))
    header("Content-Disposition: inline; filename=\"$name\"");
echo $bin_data;
Rakish's Avatar, Join Date: Jul 2006
Go4Expert Member
Thanks shabbir,
but I have a a link like:
<a href=""></a>

and when the user clicks on hyper link, the FileName is passed to download script, which triggers the automatic download process(save as popup).

I dont know how to do this.

have any idea?
Rakish's Avatar, Join Date: Jul 2006
Go4Expert Member
This is what I have and am trying to do.
the file name is passed to this script as a query string
if (length ($ENV{'QUERY_STRING'}) > 0){
      $buffer = $ENV{'QUERY_STRING'};
      @pairs = split(/&/, $buffer);
      foreach $pair (@pairs){
           ($name, $value) = split(/=/, $pair);
           $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
           $in{$name} = $value; 

my $files_location;  

$files_location = "/tmp";

if ($filename eq '') {  

print "You must specify a file to download.";  
} else {

my $data="";
open(DLFILE, "<$files_location/$filename") || Error('open', 'file'); 

while(<DLFILE>) { $data .= $_; }
close(DLFILE) || Error ('close', 'file');  

print "Content-Type: application/x-unknown\r\n";
print "Content-Disposition: attachment;$filename\r\n";
print "Content-length: ", length($data), "\r\n\r\n$data";

but i dont know how to prevent the data from being read by the browser, as if i try to download a .zip file, the browser displays weird characthers.

What i am trying to do is to let user save the file where ever they want with the

Save As pop-up

Please help,