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:

use CGI; my $query = CGI->new(); &_upload_file($query->param('my_upload_field'));
It would be best to use the upload() function on CGI, like so.
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

use CGI::Carp 'fatalsToBrowser';
at the top of your script.. that'll print out that error message.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.