Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

download a file from non-web space

by c (Hermit)
on Aug 14, 2001 at 01:51 UTC ( [id://104605]=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

Hey everyone, I posted something this morning that had some code which allowed a client to view their raw http/ftp/ssl logs for their domain hosted on a server. I'd like to give them the opportunity to download the rawfile as well. Since the file itself is not within webspace, but rather in my /var/log/ directory, I can't make a direct link to the file, which is good since I'd rather not have one client download another's logfile! A quick search and I found this which shows the code to download a file, but I am looking for an understanding of how I can do something like "Click here to download your access log" all within that same code that I mentioned before.

humbly -c

Replies are listed 'Best First'.
Re: download a file from non-web space
by tachyon (Chancellor) on Aug 14, 2001 at 03:14 UTC

    I would just add a link like this to your dynamiic page:

    <a href="/cgi-bin/myscript.pl/logfile.txt?request=download"> Click here to download</a>

    This link will call your script and pass it the name value pair request=download. If you then have the script print out the file as shown in the links you are done. The extra path info after myscript.pl gives the download file its name.

    # add this to the script if ($q->('request') eq 'download') { &DownloadFile( $filepath, $filename ); exit; } sub DownloadFile { my ($filepath,$filename) = @_; my $filesize = -s "$filepath/$filename"; print "Content-disposition: attachment; filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/octet-stream\n\n"; my $buffer; open FILE, "$filepath/$filename" or die "Oops $!"; binmode FILE; binmode STDOUT; print $buffer while (read(FILE, $buffer, 4096)); close FILE; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: download a file from non-web space
by wardk (Deacon) on Aug 14, 2001 at 02:17 UTC

    when you read the file to display it, save it off in the "webspace" as a world-readable file then create a simple HREF to it on the dynamic page. give it an extension that will get the browser to produce a file dialog for saving locally.

    there's a bit more to consider, especially if a multi-user site...make sure you clean up occasionally, all those file could eat some serious space.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://104605]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found