Download a file from webserver to local computer using browser

Discussion in 'Perl' started by Rakish, Sep 19, 2006.

  1. Rakish

    Rakish New Member

    Joined:
    Jun 30, 2006
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    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,

    Rakesh
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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
    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');

    if(
    stristr($type,"zip"))
    {
        
    header("Content-Disposition: attachment; filename=\"$name\"");
    }
    else if(
    stristr($type,"image"))
    {
        
    header("Content-Disposition: inline; filename=\"$name\"");
    }
    echo 
    $bin_data;
    exit;
     
  3. Rakish

    Rakish New Member

    Joined:
    Jun 30, 2006
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Thanks shabbir,
    but I have a a link like:
    <a href=http://xyz.com/FileName="abc.zip"> abc.zip</a>

    and when the user clicks on abc.zip 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?
    Thanks
     
  4. Rakish

    Rakish New Member

    Joined:
    Jun 30, 2006
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    This is what I have and am trying to do.
    the file name is passed to this script as a query string
    Code:
    
    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; 
          }
     }
    
    $filename=$in{'FileName'};
    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,
    Rakesh
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice