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

Hello, I have successfully uploaded a file using the following code using http through a browser:
open(OUTFILE, ">$save_directory\/$file_name"); while ($bytes = read($file_handle, $buffer, 1024)) { $bytes_read += $bytes; print OUTFILE $buffer; } close OUTFILE;
PROBLEM: It stops working (i havnt been able to trace the reason) apparently at random, and then keeps failing to open the FILEHANDLE. Any reason why the open on file handle fails?

Replies are listed 'Best First'.
Re: random problem with read
by sauoq (Abbot) on Oct 16, 2002 at 16:47 UTC

    Let Perl tell you. Check the success of your open call. When it fails, the reason will be in the special variable $!. The common idiom is open or die.

    open(OUTFILE, ">$save_directory/$file_name") or die "Couldn't open $save_directory/$file_name for write: +$!\n";
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: random problem with read
by rdfield (Priest) on Oct 16, 2002 at 16:48 UTC
    You might want to have a look at $! if the open fails.

    open(OUTFILE, ">$save_directory\/$file_name") or die "can't open $save_directory\/$file_name coz $!";

    rdfield

Re: random problem with read
by true (Pilgrim) on Oct 16, 2002 at 16:59 UTC

    I think your problem is due to you asking perl to only read a content length of 1024. It's reading up to 1024 and then it stops. Change 1024 to $ENV{'CONTENT_LENGTH'} and see if it helps. But i keep getting negative XP for talking about file uploads. My limited but travailed experience has led me to understand the read statement as follows.

    read(HANDLE, STUFF, LENGTHOFSTUFF);
Re: random problem with read
by graff (Chancellor) on Oct 17, 2002 at 06:03 UTC
    Your statement of the problem is a bit vague. It looks like you're trying to copy an input file to an output file. Is (part of) the problem that the copy stops before all the data has been written? Are you on a windows system, reading files with binary (non-text) content? If so, do you know about "binmode()"?

    In addition to checking the status returned by the open statement, you might want to print out the final value of $bytes_read for a given input file -- if it's less than the actual size of the file, try "binmode($file_handle)".