A couple of things could be going wrong.
Bizarrely-worded description of CGI upload and filehandles deleted. See Re (tilly) 2: YAF(ile)U(ploading)Q(uestion) for details.
I'm assuming that you're calling your function like so:
It would be best to use the upload() function on CGI, like so.use CGI; my $query = CGI->new(); &_upload_file($query->param('my_upload_field'));
use CGI; my $query = CGI->new(); my $uploaded_filehandle = $query->upload('my_upload_field') or print_error_page("Sorry, you must upload a file..."); &_upload_file( $uploaded_filehandle );
That way, you'll pass only a filehandle, and not have to worry about the vagaries of packages. The only problem is that it will not preserve the original name.
So, you might want to write your function like so:
use CGI; use File::Copy; ## in main... my $query = CGI->new(); my $uploaded_filehandle = $query->upload('my_upload_field') or print_error_page("Sorry, you must upload a file..."); my $filename = $query->param('my_upload_field'); &_upload_file( $uploaded_filehandle, $filename ); sub _upload_file { my ($uploaded_fh, $orig_filename) = @_; my $dest_filename = $orig_filename; $dest_filename =~ s{ ^.* ( \\ | \/ ) }{}x; copy($uploaded_fh, $dest_filename) or die "Couldn't copy file to ' +$dest_filename': $!; stopped"; }
Normally, you would NEVER want to allow user input to determine the name of a filename in a CGI script, but since it's an internal application we'll let it go this once.
Finally, you might add
at the top of your script.. that'll print out that error message.use CGI::Carp 'fatalsToBrowser';
Update: Previously, I didn't understand the question thoroughly enough before posting... my apologies.
Additional update:tilly pointed out that my wording was so confusing as to be altogether wrong.
stephen
In reply to Re: YAF(ile)U(ploading)Q(uestion)
by stephen
in thread YAF(ile)U(ploading)Q(uestion)
by skazat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |