For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > File download in background.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author File download in background.
Beginner

2006-10-24, 7:57 am

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;
}

Beginner

2006-10-24, 7:57 am

On 24 Oct 2006 at 12:12, Beginner wrote:

Sorry going to top post as I made a mistake. I was doing a straight
print and that was why the file was was being displayed.
Apologies.
Dp.


> 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.
>



> open(OUT, ">$output_file") or die "Can't open $output_file:
> $!\n";
> while ( $bytesread = read($filename, $buffer, $numbytes)) {
> print OUT $buffer;
> print $buffer;


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com