in reply to Yet another CGI Upload problem - not like the others!

Adrianh, Tachyon and Aging Acolyte, Thanks to you guys I finally got the file upload nightmare saga solved - thanks very much for your help, all. I put in all the error checking that you suggested & re-wrote the code in a mini version that finally began to work, to my surprise & delight. The working bit of code looks like this:
open(OUTFILE, ">$UPLOAD_DIR\/$file_name") or die "Upload failed ($!)"; binmode $fh; binmode OUTFILE; while ($bytes=read($fh, $buffer, 1024)) { $bytes_read += $bytes; print OUTFILE $buffer; } die "read failure ($!)" unless defined($bytes);
I think the last line is that bit that made the crucial difference. I didn't know that about 'read'. Anyway, thanks again, your help is very much appreciated. Wish I could get you all a Monastery mead!

Replies are listed 'Best First'.
Find out what the bug was (was Yet another CGI Upload problem - not like the others!)
by adrianh (Chancellor) on Dec 11, 2002 at 22:04 UTC
    I think the last line is that bit that made the crucial difference. I didn't know that about 'read'.

    I'm glad you've got everything working, but I doubt that the check for read failure was the cause of your problems.

    Annoying general advice follows... please feel free to ignore :-)

    Unless what you're doing is urgent, I would spend the time and figure out the exact cause of the problem. Take your failing code and add some print statements to find out what's going on, remove code that's not relevant, etc. until the error goes - then back up a step. Look at what caused the error. Try and see why you did it that way - that way you can avoid the problem in the future.

    Also, unless you know exactly what caused the error - you don't know 100% that you've fixed it. Maybe there is an edge case somewhere that you're missing?

    Learning from your mistakes is a terrible cliche I know, but it's true nevertheless. It's better to learn how to not put the bugs in, rather than learn how to fix the bugs :-)