You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Ian Mahuron <im...@bro-net.com> on 2000/08/15 20:36:33 UTC

Apache::Upload buffering issues?

What is the proper way to take the filehandle provided by $upload->fh and write the file to disk?  I seem to be having buffering
issues w/ the following (file is truncated):

if (open(OUTFILE, ">$u_fullpath")) {
	print OUTFILE $buffer while read($fh, $buffer, 8*1024);
} else {
	...
}

a simple $| = 1; before the print didn't seem to help.. maybe OUTFILE is not hot?  Dunno.. this is my first run in w/ buffering
issues.  I hope someone can help.

TIA

Ian


Re: Apache::Upload buffering issues?

Posted by ___cliff rayman___ <cl...@genwax.com>.
Ian Mahuron wrote:

> What is the proper way to take the filehandle provided by $upload->fh and write the file to disk?  I seem to be having buffering
> issues w/ the following (file is truncated):
>
> if (open(OUTFILE, ">$u_fullpath")) {
>         print OUTFILE $buffer while read($fh, $buffer, 8*1024);
> } else {
>         ...
> }
>
> a simple $| = 1; before the print didn't seem to help.. maybe OUTFILE is not hot?

select((select(OUTFILE), $|++)[0]);  #unbuffer OUTFILE and return select to prev state

> Dunno.. this is my first run in w/ buffering
> issues.  I hope someone can help.

i see u are not checking for errors on open. u should.
u should also check for errors on close.  if u get an error on close, then the file you are closing is probably not going to be
written with all data.

>
>
> TIA
>
> Ian

hth,

--
___cliff rayman___cliff@genwax.com___http://www.genwax.com/



Re: Apache::Upload buffering issues?

Posted by "Ken Y. Clark" <kc...@boston.com>.
On Tue, 15 Aug 2000, Ian Mahuron wrote:

> What is the proper way to take the filehandle provided by $upload->fh and write the file to disk?  I seem to be having buffering
> issues w/ the following (file is truncated):
> 
> if (open(OUTFILE, ">$u_fullpath")) {
> 	print OUTFILE $buffer while read($fh, $buffer, 8*1024);
> } else {
> 	...
> }
> 
> a simple $| = 1; before the print didn't seem to help.. maybe OUTFILE is not hot?  Dunno.. this is my first run in w/ buffering
> issues.  I hope someone can help.
> 
> TIA
> 
> Ian
> 
> 

here's what i do in one of my modules:

    my $file_size = $upload->size || 0;
    if ($file_size == 0) {
        return $self->err_out("You didn't supply a valid file.");
    }

    #
    # write the data to file, making sure what gets
    # written is the same as what we expected
    #
    my $umask = umask 02;
    eval {
        no strict 'refs';
        my $fh     = $upload->fh or die 'No file handle';
        my $data   = '';
        my $length = 0;
        {
            local *F;
            open(F, "> $file_path") or die "Failed to open '$file_path': $!";
            while ( read($fh, $data, 4096) ) {
                print F $data;
                $length += length($data);
            }
            close F;
        }
        if ($length != $file_size) {
            die "Failed to write '$file_path' to file";
        }
    };

    #
    # reset umask
    #
    umask $umask;

hth,

ky