![CGI? Loser!](/img/camel.gif)
Here’s a quick note to me on how to create a CGI script that streams a process to the browser in such a way as to prompt the user for a “save as” box. This is technique is older than dirt, but still useful.
#!/usr/bin/perl -- use strict; use CGI; use POSIX q[strftime]; my $q = CGI->new; if (open my $in, "/usr/bin/zip -r - logs/ |") { my $filename="lhp_logs-".strftime("%Y%m%d", localtime()) . ".zip"; print $q->header({ "-type"=>"application/x-unknown", "-Content-Disposition" => "attachment; filename=$filename", } ); my $buf =""; while (read($in, $buf, 4048)) { print $buf; } } else { die "Oops: $!"; }
The error checking isn’t particularly robust here. Please gold-plate this code as needed for your next review.