Deleting tmp-files of CGI.pm upload()

J

Jo Oberman

Hello,

I'm using CGI.pm to make some file uploads to the web-server.
It works great, but the problem is that I'm not able to delete
the temp--files that are created during upload.

Here is the code that makes the problem:

1) my $query = new CGI();
2) my $fh = $query->upload('FileInputName');
3) File::Copy::copy(*{$fh}, "../myDir/myFile.txt") or die "Can't copy uploaded picture '$fh': $!"; # that works
4) unlink(*{$fh}) or die "Can't remove uploaded temp file: $!"; # that does't work

In line 4) the unlink always failed with the message "No such file or directory".
(But the file cgitempXXX in temp-directory is still there!)
If I change line 3) into "File::Copy::move(.....)" I get the message "no permission ...".

What is the right way to move (== copy and delete) the temp-upload-file to
my own destination ???

Using currently for testing: perl 5.6.1 CGI.pm V2.93 on an Windows XP - Box

Thanks for helping
Jo
 
M

Malcolm Dew-Jones

Jo Oberman ([email protected]) wrote:
: Hello,

: I'm using CGI.pm to make some file uploads to the web-server.
: It works great, but the problem is that I'm not able to delete
: the temp--files that are created during upload.

: Here is the code that makes the problem:

: 1) my $query = new CGI();
: 2) my $fh = $query->upload('FileInputName');
: 3) File::Copy::copy(*{$fh}, "../myDir/myFile.txt") or die "Can't copy uploaded picture '$fh': $!"; # that works


: 4) unlink(*{$fh}) or die "Can't remove uploaded temp file: $!";

I would always include the file name in the error message, so here I might
try

die "Can't remove uploaded temp file: $!, ",*{$fh};

to see what it is was I was not able to delete.

Also, I wonder (and by this I mean I do not know and would look it up), I
wonder if you need to delete the CGI temp file at all? I think the module
could delete the file while it has an open file handle and the data would
not disappear until the last file handle is closed (even though the file
is no longer "visible" to directory listings). (But look it up).
 
B

Ben Morrow

: 4) unlink(*{$fh}) or die "Can't remove uploaded temp file: $!";

I would always include the file name in the error message, so here I might
try

die "Can't remove uploaded temp file: $!, ",*{$fh};

Unless I am very much mistaken, that is not a filename at all but a
filehandle. This will be why you can't unlink it: perl will be
attempting to unlink the stringification of the filehandle, which is
unlikely to be terribly successful.

Ben
 
M

Malcolm Dew-Jones

Ben Morrow ([email protected]) wrote:

: (e-mail address removed) (Malcolm Dew-Jones) wrote:
: > : 4) unlink(*{$fh}) or die "Can't remove uploaded temp file: $!";
: >
: > I would always include the file name in the error message, so here I might
: > try
: >
: > die "Can't remove uploaded temp file: $!, ",*{$fh};

: Unless I am very much mistaken, that is not a filename at all but a
: filehandle. This will be why you can't unlink it: perl will be
: attempting to unlink the stringification of the filehandle, which is
: unlikely to be terribly successful.

: Ben

Exactly. If he had included the value he was trying to delete in the error
message I think he would have quickly discovered that.
 
D

David Efflandt

Unless I am very much mistaken, that is not a filename at all but a
filehandle. This will be why you can't unlink it: perl will be
attempting to unlink the stringification of the filehandle, which is
unlikely to be terribly successful.

Whatever it is, it is a read-once filehandle. So it can be read once and
saved, but that is it. It is removed automatically, so there is no need
to attempt that.
 
J

Jo Oberman

.... saved, but that is it. It is removed automatically, so there is no need
to attempt that.

Unfortunately, not.
Using my configuration (Windows XP, Apache 1.3.27, Perl 5.6.1, CGI.pm V2.93),
the CGItemp-files are left in my directoy c:\temp.
So I have to help myself.

Any idea's how to delete them?

Thanks
Jo
 
B

Ben Morrow

Jo Oberman said:
Unfortunately, not.
Using my configuration (Windows XP, Apache 1.3.27, Perl 5.6.1, CGI.pm V2.93),
the CGItemp-files are left in my directoy c:\temp.
So I have to help myself.

Any idea's how to delete them?

my $TMPFH = $Query->upload(...);
my $tmpfilename = $Query->tmpFileName($TMPFH);

For some reason this is undocumented... it doesn't seem to be
'internal', or anything...

Ben

[don't include a full quote of the article you're replying to]
 
M

Malcolm Dew-Jones

Ben Morrow ([email protected]) wrote:

: > > .... saved, but that is it. It is removed automatically, so there
: > >is no need to attempt that.
: >
: > Unfortunately, not.
: > Using my configuration (Windows XP, Apache 1.3.27, Perl 5.6.1, CGI.pm V2.93),
: > the CGItemp-files are left in my directoy c:\temp.
: > So I have to help myself.
: >
: > Any idea's how to delete them?

: my $TMPFH = $Query->upload(...);
: my $tmpfilename = $Query->tmpFileName($TMPFH);

: For some reason this is undocumented... it doesn't seem to be
: 'internal', or anything...


Also, the file handle variable used to be the file name, perhaps that is
still true.

The original unlink couldn't work because it was not given a file `name'.
The value was not a file name because it wasn't the scalar (in this case
string) value, which was because the syntax used was wrong. If the syntax
had been correct then you might get a string that would be the file name
and be able to unlink it. (That was how it worked in the past.)

I cannot say off hand what syntax is needed to get the name value from the
file handle variable, something like ${$fh} might work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top