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

No clue where the error is but the files are NOT uploading. Right now the text at the bottom doesn't even show up, it only shows up if I remove the only "or die" statement I have which is kinda confusing. My guess is the directory isn't opening but I'm not getting errors and the die isn't working.
use warnings; use CGI qw/:standard/; use POSIX; print header, start_html('upload form'); print start_form(), table( Tr( td("File: "), td(filefield(-name=>'upload', -size=>50, -maxlength=>80), ), ), Tr( td(), td(submit('button','submit'), ) ) ), end_form(), hr; if (param()) { my $upload = param('upload'); open (OUTFILE,">>/home/myname/public_html/upload") or die "cannot +open directory for writing $!"; while ($bytesread=read($upload,$buffer,1024)) { print OUTFILE $buffer; print "the thing is open"; } print "The file should be uploaded if everything went right, which it probably didn't.\n"; }

Replies are listed 'Best First'.
Re: file uploading
by Mr. Muskrat (Canon) on Apr 15, 2003 at 17:01 UTC
    I suggest that you read the file upload section of the CGI.pm documentation once more to ensure that you understand the process. In order to write out the uploaded file, you should open a file for writing, not a directory.
      Ok, I guess that would be a problem then. Then I am more confused. How can you open a FILE for writing/uploading? I don't understand how you can have a form where a user uploads images and have those saved to a .txt or .else files. That's why I figured I'd set it to a directory. How do you save the uploaded file inside of another file?
        It is well documented in the CGI documentation as well as here in the Monastery. Remember that Super Search is your friend.
Re: file uploading
by perlplexer (Hermit) on Apr 15, 2003 at 17:03 UTC
    Right now the text at the bottom doesn't even show up, it only shows up if I remove the only "or die" statement I have which is kinda confusing
    Why is that confusing? That indicates that your script is die()ing after calling open().
    What is "/home/myname/public_html/upload" ? Is that really a directory? You can't open() directories; opendir() does that but that's not what you need anyway. You need to provide a legitimate file name for open().
    I don't know what kind of requirements you have there. You could either use whatever the user is supplying (after stripping off the path and troublesome characters; e.g., pipes, etc. Or you can give it your own name based on whatever requirements you have...

    --perlplexer