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

I am having a problem with the CGI.pm module's file upload routines. It seems that everytime a file is uploaded it is written to a tempfile in the first temp dir it finds. In this case its c:\temp. The problem I am having is that those files never get deleted. It looks like the author of the module attempted to unlink them in the destroy of the package Tempfile but I am assuming that is not running because of some win32 issue. Thus, the m$ admin is all over my case about why "his temp dir" is filling up with so much useless data and hanging around forever. Any help here would be great!

Replies are listed 'Best First'.
Re: CGI tempfiles w2k/IIS/AS
by robartes (Priest) on Oct 25, 2002 at 08:57 UTC
    CGI.pm is silent when the unlink of a temp file fails. Perhaps you could add a warning to the destructor of CGITempFile and see what comes floating up? In my version of CGI.pm (2.81) that would entail changing line 3373 to:
    unlink $$self or warn "Something wicked: $!\n";
    That should give you an idea as to what went wrong in the unlinking.

    CU
    Robartes-

      This did reveal the error: Permission Denied. I checked the directory and "everyone" has "full control" in it. I am thinking that maybe the module is trying to delete the temp file while it still has an open handle on it. I don't even know where that handle is. The module API provides a funky reference to the handle that seems to be some sort of alias. Can anyone think of any other reason that permission would be an issue? Does anyone know how to close the handle?
Re: CGI tempfiles w2k/IIS/AS
by relax99 (Monk) on Oct 25, 2002 at 14:25 UTC

    Consider using CGI::Minimal for your form processing and file uploads. As far as I know it does not create any temp files, but instead stores uploaded data in memory.

    If you do not need html-generating capabilities of CGI.pm (which is a bad idea in most cases anyway), you might like CGI::Minimal. NOTE: Be sure to initialize your $cgi object as shown below, otherwise CGI::Minimal never finishes - at least that was the case for me on my w2k system.

    use CGI::Minimal; binmode STDIN; # only do that if you want file-uploads to work my $cgi = CGI::Minimal->new;
Re: CGI tempfiles w2k/IIS/AS
by blahblahblah (Priest) on Oct 27, 2002 at 00:26 UTC
    If it helps at all, you can tell CGI.pm to use a specific directory for its temp files like this:
    $CGITempFile::TMPDIRECTORY = $myTempDir;
    That way you could put them in your own directory and delete them yourself without bothering your admin. I think it will work as long as you just include that line before you create a CGI object, but maybe you need to put it in a BEGIN block.

    If you're still using an older version of CGI.pm, the package name may be different, like this:

    $TempFile::TMPDIRECTORY = $myTempDir;