http://www.perlmonks.org?node_id=1049533

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

Hi Monks!

I would like to have your advice on the following problem:

I am making a small webapp and my purpose is to create a number of text files, compress them, download as zip file and erase.

For the moment I've been able to create the directory with the file, put them in a zip file and to download, but I can't erase the zip file. Well..It's not totally true. The problem is that if I do it, it compromises the download. My question would then be : Ho can I postpone the file elimination or, how can I check that the file has been totally completed before I erase it?

I know about the existence of File::Temp, but as I need this zip file just once, I think it would be simpler to just delete it. Here is a part of the code

sub write_and_download{ [...] $self->generate_zip(); $self->download_file(); my $filename = "temporary/" . $projectname . ".zip"; unlink $filename; }; sub download_file{ my $self = shift; my $downloadfile = $projectname . ".zip"; my $fullpathdownloadfile = "temporary/$downloadfile"; my $output = ''; my $buffer = ''; open my $fh, '<', $fullpathdownloadfile or return error_handler("Error: Failed to download file <b>$down +loadfile</b>:<br>$!<br>"); while (my $bytesread = read($fh, $buffer, 1024)) { $output .= $buffer; } close $fh or return error_handler("Error: Failed to download file <b>$down +loadfile<b>:<br>$!<br>"); my $downloadfilesize = (stat($fullpathdownloadfile))[7] or return error_handler("Error: Failed to get file size for <b>$ +downloadfile<b>:<br>$!<br>"); $self->header_props( '-type' => 'application/x-downl +oad', '-content-disposition' => "attachment;filename +=$downloadfile", '-content_length' => $downloadfilesize, ); return $output; };

I thank you from now for your help