in reply to I've got a problem with the file upload code

I don't know what you mean by saying "the program would freeze"; it looks like you're getting the "program done" printout at the end, so isn't freezing.

If you're using Windows, you might try using binmode() on your file handle (as well as checking for whether the upload succeeded):

if (!defined($file_handle)) { ... handle it ... } $file_handle->binmode;

The reason you got an empty file when you did this: print OUTFILE $file_handle; is probably that $file_handle was undef. If it were a valid file handle, you'd have gotten something like "IO::File=GLOB(0x83b3ca4)" in your file (you can't print a file handle to another file and expect the file to get copied).

Also, if you're on Windows, make sure to binmode OUTFILE too.

Replies are listed 'Best First'.
Re: Re: I've got a problem with the file upload code
by Agermain (Scribe) on Jun 27, 2001 at 22:40 UTC

    Thanks for the quick response!

    I added binmode OUTFILE; to the beginning of the program, since I wasn't quite sure what it did. I also added the line you mentioned, right after and was indeed getting an undefined $file_handle. What might cause it to be undefined, though? I don't understand why it wouldn't work, since I'm using the same code that was quoted for that section.

    For reference, I have CGI.pm 4.72, and have tried this with ActivePerl on Win98SE, and with FreeBSD, and both give the same results.

      You want to put the binmode after the open of the file. You also probably want to add a binmode for $file_handle.

      An undef $file_handle means that the upload failed, for some reason. If you use CGI::Carp you can see the warnings and/or fatal errors from your script.

        Thanks again for your help, bikeNomad. I think I finally figured out what was malfunctioning in Ovid's original code.

        • I used my $file_handle = $req->upload("FILE$onnum"); instead of my $file_handle = $req->upload($file);
        • I used open(OUTFILE, ">" . UPLOAD_DIR . $fileName) instead of the sysopen command. Not sure WHY it worked, but it worked.

        I think the thing that confused me most was that $file_handle was acting like both a standard scalar string and a filehandle, since I could print $file_handle and get a readable filename/directory - most of the times if I see a reference, it looks like... a reference.

        CGI::Carp definitely helped out... It's much more useful to get error feedback instead of a vague "this didn't work" error! Thanks again for your help!!