Hi All,
I posted this to this list as it's as much a fork() issue as a CGI
one. Hope I've done the right thing.
I am trying to display a html page and at the same time download a
file from a multi-part form. The files are quite large to I wanted to
minimise the time it took for the browser to render the page by
having the file download happen in the background as it's not
required for the content.
I thought that fork() would be the function for this but my efforts
are pouring the content into the browser window (not very pretty!). I
read that fork() inherits the file descriptors from the parent and I
guess this is why the file is being displayed. The file is copied to
the remote server so I am half-way there.
Can anyone advice me how best to achieve this? Is fork() the right
function in a CGI environment? Can I avoid the output going to the
browser?
An abridged version of my effort is below. Any tips are much
appreciated.
TIA.
Dermot.
=============
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
# $CGI::POST_MAX = 1024 * 10000; # Set limit to 10MB
$| = 1; # Not sure if this is necessary.
my $q = new CGI;
print $q->header()
print $q->start_html();
# The file to download.
my $filename = $q->param('file-path');
my $dsc_name = $q->param('basename');
my $childpid;
if (! defined($childpid = fork()) ) {
die "Cannot fork for file download: $!\n";
}
elsif ($childpid == 0) {
my $untainted_filename;
if (! $filename && $q->cgi_error) {
print $q->header(-status=>$q->cgi_error);
exit 0;
}
# Remove the spaces to avoid name issues.
(my $tmp_name = $dsc_name) =~ s/\W+/_/g;
if ($tmp_name =~ /^([-\@:\/\\\w.]+)$/) {
$untainted_filename = $1;
}
else {
die <<"EOT";
Unsupported characters in the filename "$tmp_name".
Your filename may only contain alphabetic characters and numbers,
and the characters '_', '-', '\@', '/', '\\' and '.'
EOT
}
my $output_file = "/tmp/"."$tmp_name".".tmp";
my ($bytesread,$buffer);
my $numbytes = 1024;
open(OUT, ">$output_file") or die "Can't open $output_file:
$!\n";
while ( $bytesread = read($filename, $buffer, $numbytes)) {
print OUT $buffer;
print $buffer;
}
close(OUT);
}
else {
print "Your file is on it's way in the meantime.....";
print $q->end_html;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>