LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Perl/CGI uploader program - temp file not getting deleted (http://www.linuxquestions.org/questions/programming-9/perl-cgi-uploader-program-temp-file-not-getting-deleted-482728/)

anroy 09-12-2006 09:17 AM

Perl/CGI uploader program - temp file not getting deleted
 
I have a Perl CGI program to upload zipped files to a server directory.

A temporary file is created in the /var/tmp/ directory. From my Googling I have learned that this file is supposed to get deleted automatically.

In my case this is not happening, hard disk space is getting squeezed. My client had the system up for over a month and they suddenly ran out of disk space. That is when I got the call. :)

The uploading itself is working fine.

The environment is Apache running on Linux.

My questions:

1) What could possibly be causing this? Is there a setting somewhere, either in CGI, or Apache, or in the OS?

2) If the cause cannot be determined, how do I delete this each time the upload finishes?

Question 1 is the more important of the two. I would rather know the root cause and tackle that, than apply a bandaid solution.

Here is my code.

Form
Code:

<form action="do_upload.cgi" enctype="multipart/form-data" method=POST>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<input type="hidden" name="action" value="DoUpload">
Select file  <input type="file" name="uploadFile" size="32"><br>
<input type="submit" value="Upload!">
</form>

Uploader code
Code:

#!/usr/bin/perl
use strict;
use CGI ':standard';

my $dirName = "ZIPData";
my $uploadFile = param( 'uploadFile' );

my @pathArr = split( /\\/, $uploadFile );
my $pathEltCnt = scalar( @pathArr );
my $fileName = $pathArr[$pathEltCnt-1];

# various validation here

open ( UPFILE, "> $dirName/$fileName" ) || die("Cannot open($fileName): $!");
binmode( UPFILE );
my ( $data, $chunk );
my $fileSize = 0;
while ( $chunk = read ( $uploadFile, $data, 1024 ) ) {
        print UPFILE $data;
        $fileSize += $chunk;
}
close ( UPFILE ) || die("Cannot close($fileName): $!");

Thanks!

theNbomr 09-12-2006 11:29 AM

AFAIK, this 'automatic' cleaning is normally performed by a cron job, possibly running tmpwatch or some similar tool. Are you sure this is running? Is it running frequently enough? You will need to find a balance between how long to leave the files on the temporary directory, and how long they are required for your application.

--- rod.

anroy 09-13-2006 02:09 AM

Quote:

Originally Posted by theNbomr
AFAIK, this 'automatic' cleaning is normally performed by a cron job, possibly running tmpwatch or some similar tool. Are you sure this is running? Is it running frequently enough? You will need to find a balance between how long to leave the files on the temporary directory, and how long they are required for your application.

--- rod.

OK, I had no idea that this was the case. I was under the impression that it gets deleted automatically after I do the close ( UPFILE ).

If it is a cron job scheduled by the system administrators then I would have no control over that as it is my client's site. And they use a large commercial hosting company so they would have nothing to do with it either.

So I guess I should definitely delete it myself. I am not sure how to though, the unlink command requires a file name. Does anyone know how to determine the file name of this temp file at runtime? It seems to be "CGI" with a string of digits appended to it, different each time.

chrism01 09-13-2006 02:45 AM

Well, surely this is your filename: $dirName/$fileName ?
iirc, if you unlink the file after you open it (but before you close it), in the same prog, the file will continue to exist until you close it, at which pt it will 'auto-magically' get deleted ie unlinked.

anroy 09-13-2006 03:33 AM

Quote:

Originally Posted by chrism01
Well, surely this is your filename: $dirName/$fileName ?

No, that is the destination file, the result of the upload. I certainly don't want to delete that!

Just to make sure, I tried it and that was indeed the case. It has nothing to do with the temporary file.

Edit: By the way I am not using the object-oriented CGI.pm, like this line:
$tmpfilename = $query->tmpFileName($filename);

I am not sure it is available to me.


All times are GMT -5. The time now is 03:04 AM.