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

Yet Another Upload Question: I've been surfing this site and google hoping to find a simple solution for uploading files (of any size or type) to a web server. I wrote some code that creates the appropriate file on the remote server, but when I FTP to my server to check on the file it is 0kb in size. What am I doing wrong? Also, what type of checking should I use to assure the file is uploaded correctly? Thanks.
#!/usr/bin/perl -w use strict; use CGI; my $dir = '../billimages/'; &performUpload($dir); sub performUpload { my ($dir) = @_; my $cgi = new CGI; my $file = $cgi->param("file"); my $i; open (LOCAL, ">$dir$file") or dieNicely("Unable to upload: $!"); while (read($file, $i, 1024)) { print OUT $i; } close (LOCAL); dieNicely("Upload Complete"); } sub dieNicely { print "Content-type: text/html\n\n"; print "$_[0]"; }
edited by boo_radley - title change (was : YAUQ)

Replies are listed 'Best First'.
Re: YAUQ
by pfaut (Priest) on Jan 15, 2003 at 02:02 UTC

    You open LOCAL, but then write to OUT.

    open (LOCAL, ">$dir$file") or dieNicely("Unable to upload: $!"); while (read($file, $i, 1024)) { print LOCAL $i; } close (LOCAL);
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: YAUQ
by Zaxo (Archbishop) on Jan 15, 2003 at 02:03 UTC

    Line 18, print OUT $i; should be print LOCAL $i;. OUT is not an open file handle.

    After Compline,
    Zaxo

Re: YAUQ
by emilford (Friar) on Jan 15, 2003 at 02:31 UTC
    Damn, I knew I shouldn't have taken that extra dose of NyQuil before writing this code. :-) Gee, that was a dumb mistake.

    So my next question is why an error wasn't reported when I attempted to use a non-existent filehandle? Shouldn't strict pick up on that?

    Also, what error checking should I perform to assure the file was uploaded correctly? Checking the file exits and has a size greater that 0kb? Would that suffice?

    Thanks again, and my apologies for the careless error. :)

      strict doesn't apply to file handles but if you had warnings on, you might have seen something like Name "main::OUT" used only once: possible typo at ... or maybe even print() on unopened filehandle OUT at ....

      Update: I see you did have warnings on but since this was a CGI script, those messages are probably in your web server's error log. You might want to use CGI::Carp qw(fatalsToBrowser warningsToBrowser); at least during development so that you'll see these messages.

      --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
        And remember that you have to manually force the warnings to be output by calling warningsToBrowser(1); - in the simplest case, just do it before the script exits, so you get a cumulative list of all the warnings as a neat list of HTML comments at the bottom of the CGI generated page.

        Makeshifts last the longest.

        That makes sense. I will check my server's error log to see if there is any mention of this error.

        Having error messages printed to the browser would have saved me from posting my careless mistake. :) I'll start using CGI::Carp qw(fatalsToBrowser warningsToBrowser) to help check my code. Although I do recall compling this bit of code from the command line using perl -c upload.pl and it simply responded with perl syntax OK.

        Thanks