File::Temp worked out well, and was surprisingly easy to learn. Actually the whole script turned out to be surprisingly short and simple to write. I don't know why you ever let me waste time with that other language at all!
As short as it is, this is my most complex Perl project yet and there's one part I just can't find an easy solution for. Here's the html that calls the cgi:
<html>
<body>
<form action='../cgi-bin/maketar.cgi' method='post'>
<input type=checkbox name="1.text">1<br>
<input type=checkbox name="2.text">2<br>
<input type=checkbox name="3.text">3<br>
<input type=checkbox name="4.text">4<br>
<input type=checkbox name="5.text">5<br>
<input type=submit>
</form>
</body>
</html>
And here is the actual script. It's shorter and pretier, but I can't get it to force a download of the new file (oh, I should mention that on advice of fglock I went to Archive::Tar instead of Archive::Zip):
#! /usr/bin/perl/bin/perl -wT
use CGI;
use strict;
use File::Temp;
use Archive::Tar;
# Create CGI object and make array or params
# The name will come from the HTML checkbox tags
my $q = CGI->new();
my @selectedFiles = $q->param;
# Create random $filename and .tar file with that name
(my $fh, my $filename) = File::Temp::tempfile( "img_XXXX", SUFFIX=>'.t
+ar' );
my $tar = Archive::Tar -> new ( $filename );
# Add the files to the .tar file and write it to disk
$tar->add_files ( @selectedFiles );
$tar->write ( $filename, 0 );
###### Here's the Trouble ######
# Send the .tar file to the browser
print $q->redirect( $filename );
################################
# Erase the .tar file from the server
File::Temp::unlink0($fh, $filename) or die "Error unlinking $filename
+safely";
CGI::Push isn't going to do it, I don't think. What happens with the code as is, using Indigo Perl's Apache bundle locally on my machine the script creates a .tar file, tries to display the contents, can't and the script runs again, creating an infinite (until I intervene) number of .tar files in the cgi-bin directory. On my webserver it makes one .tar file, then shuts down. RTFMing is proving quite educational, but I'm not seeing a way to open a download file box. I could do something like:
print $q -> start_html,
$q -> a ( "$filename", $filename ),
$q -> end_html;
but I'd like to avoid that if possible. Any way I could?
Cheers,
-P
Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
-Howard Aiken
| [reply] [d/l] [select] |
use File::Copy;
# generate tar file, path is in $filename
print $q->header( 'archive/tar' );
copy( $filename, \*STDOUT );
END
{
unlink $filename;
}
| [reply] [d/l] |