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

Script checker says ok on this upload script, but when i check my ftp space, no files show up. meow?
<HTML> <HEAD></HEAD> <BODY> <FORM ACTION="http://www.yoursite.com/cgi-local/up_load.cgi" METH +OD="post" ENCTYPE="multipart/form-data"> Photo to Upload: <INPUT TYPE="file" NAME="photo"> <br><br> <br><br> <INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form"> </FORM> </BODY> </HTML>
#!/usr/bin/perl -w use CGI; $upload_dir = "/dir1/dir2"; $query = new CGI; $filename = $query->param("photo"); $filename =~ s/.*[\/\\](.*)/$1/; $upload_filehandle = $query->upload("photo"); open UPLOADFILE, ">$upload_dir/$filename"; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; print $query->header ( );

Replies are listed 'Best First'.
Re: upload script
by b10m (Vicar) on Dec 21, 2003 at 10:32 UTC

    The scripts you posted works fine over here, no errors, so I take it your "ftp space" (whatever that means ;-) isn't the same as "$upload_dir"?

    To simply test things, why not use some more debugging information, such as:

    open (UPLOADFILE, ">$upload_dir/$filename") || die ("Can't open $uploa +d_dir/$filename: ".$!);
    And print the path, just so you know where the stuff got stored:
    print $query->header, $query->start_html, $query->h1("File got saved in $upload_dir/$filename"), $query->end_html;

    Updated: minor markup ...

    --
    b10m
Re: upload script
by jZed (Prior) on Dec 22, 2003 at 02:19 UTC
    Might the file that is being uploaded by an image or an mp3 or some other binary file? If so your reading routing ought to look something more like this:
    my $filehandle = $q->param('file'); my($buffer,$bytesread,$has_data); if( $cfg{filetype} eq 'binary' ) { open(OUTFILE,">$cfg{savename}") or return("Couldn't open '$cfg{savename}' for writing: $!" +); binmode OUTFILE; while ( $bytesread=read($original_name,$buffer,$cfg{max_bytes} +)) { print OUTFILE $buffer; $has_data++; } close OUTFILE or return("Couldn't close '$cfg{savename}': $!"); if(!defined $bytesread) { return("$!"); } } else { open(OUTFILE,">$cfg{savename}") or return("Couldn't open '$cfg{savename}' for writing: $!" +); while (<$original_name>) { print OUTFILE; $has_data++; } close OUTFILE or return("Couldn't close '$cfg{savename}': $!"); } if( !$has_data ) { return(qq/ Nothing in '$original_name'.<br> It either doesn't exist, or is empty.' /); } return '';
    You should also have something like this unless you like DOS attacks:

    $CGI::POST_MAX = $cfg{max_bytes};