in reply to Re: How do I delete temporary upload files?
in thread How do I delete temporary upload files?

A general thing with files under Windows is, that they can't be deleted as long as they are open. So it might be useful to close the filehandles to the temporary files before trying to delete them. I don't know whether this applies here though.

perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

Replies are listed 'Best First'.
Re: Re: Re: How do I delete temporary upload files?
by PriNet (Monk) on Oct 20, 2003 at 06:02 UTC
    i wrote the script... apache web server, files are being created in the windows(98) directory that the script resides in... heres part of the code...
    if ($Data[3] eq "recieve") { $Data[11] = $fHandle->upload($Label[11]); $Data[12] = $fHandle->upload($Label[12]); if ((length($Data[11]) > 4)&&(lc(substr($Data[11],length($Data[11]) + - 4)) eq "\.jpg")) { open (FULLSIZE, ">".$Root."/records/classifieds/fullsize/".$Data +[2]."/".$Data[10]."\.jpg"); binmode(FULLSIZE); while (read($Data[11], $fBuffer, 1024)) {print FULLSIZE $fBuffer +} close (FULLSIZE); if ((length($Data[12]) > 4)&&(lc(substr($Data[12],length($Data[1 +2]) - 4)) eq "\.jpg")) { open (THUMBNAIL, ">".$Root."/records/classifieds/thumbnail/". +$Data[2]."/".$Data[10]."\.jpg"); binmode(THUMBNAIL); while (read($Data[12], $fBuffer, 1024)) {print THUMBNAIL $fBu +ffer} close (THUMBNAIL); goto RecieveDone; } copy($Root."/records/classifieds/fullsize/".$Data[2]."/".$Data[1 +0]."\.jpg", $Root."/records/classifieds/thumbnail/".$Data[2]."/".$Dat +a[10]."\.jpg"); } RecieveDone: $Data[11] = $Data[12] = ""; @Data = sub::clearcarry(0,@Data); @Data = sub::pushcommand("preview",@Data); goto AllDone; }


    you mean there's any easier way?

      Your code is very confusing, you should not name a namespace sub, and you should give your variables more meaningfull names than @Data!

      I also suspect that it is possible to pass you in filenames for the images such as ..\..\..\..\..\..\..\autoexec.bat\0 to overwrite your autoexec.bat file - you should use taint mode on your CGI scripts! Also, you should give out your own names (like 000000.jpg) instead of trusting on anything that a user supplies.

      From what I understood of the code, you open the files (via $fHandle->upload()), but you never close either $Data[11] or $Data[12]. Try closing them and the temporary files should be deleted automatically.

      I tried to rewrite your code a bit to make it clearer to me, but I couldn't test it:

      my $mode = $Data[3]; my $user = $Data[2]; my $imagename = $Data[10]; if ($mode eq "recieve") { my ($name_fullsize,$name_thumb) = ($Label[11],$Label[12]); my $fh_fullsize = $fHandle->upload($name_fullsize); my $fh_thumb = $fHandle->upload($name_thumb); if ($fh_fullsize =~ /\.jpg$/i) { open (FULLSIZE, ">".$Root."/records/classifieds/fullsize/$user/$ +imagename.jpg"); binmode(FULLSIZE); while (read($fh_fullsize, $fBuffer, 1024)) {print FULLSIZE $fBuf +fer} close (FULLSIZE); close $fh_fullsize; if ($fh_thumb =~ /\.jpg$/i) { open (THUMBNAIL, ">".$Root."/records/classifieds/thumbnail/$u +ser/$imagename.jpg"); binmode(THUMBNAIL); while (read($fh_thumb, $fBuffer, 1024)) {print THUMBNAIL $fBu +ffer} close (THUMBNAIL); } else { copy($Root."/records/classifieds/fullsize/$user/$image +name.jpg", $Root."/records/classifieds/thumbnail/$user/$imagename.jpg +"); } # $Data[11] = $Data[12] = ""; @Data = sub::clearcarry(0,@Data); @Data = sub::pushcommand("preview",@Data); goto AllDone; }
      perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
        this was just the nitty gritty of the code, i check filenames and extensions prior to this section. thanx for the concern... oh... can you tell i started with BASIC ?... that's probably why my code looks like it does... i'll look at your example and see if i can learn from it as well. now; the solution... what i'm missing is a simple
        close($Data[12]);
        is what i'm slipping on? this was my first attempt at uploading pictures for my classifieds page, and am still a little unfamiliar with the "$fHandle->upload()" function.

        you mean there's any easier way?