in reply to How to send a file vith apache?

It's really quite simple:
if (can_send($file)) { # we assume you can program whatever rules you +need here open(INP,"<$file") || die "Can not open $file:$!\n"; # but handle t +hat in can_send my MIME::Types $types = MIME::Types->new; my $mime = $types->mimeTypeOf($file) || "application/octet-stream"; print header("Content-type: $mime"); binmode(INP); binmode(STDOUT); while (<INP>) { print $_; } close(INP); }

Replies are listed 'Best First'.
Re: Re: How to send a file vith apache?
by sgifford (Prior) on May 01, 2004 at 16:04 UTC

    That works, but I don't like the use of the <> diamond operator to deal with binary files. If there aren't any newline characters in the file, you'll read it all in one go; if there are many of them you'll read a line and print it many times.

    I think it's better to use the simple read function for this:

    while (read(INP,$buf,8192)) { print $buf; }

    Much more predictable behavior.

      Well, if you use sysread, you should also use syswrite instead of print. That way you don't have to bother with binmode, etc. And somehow, it seems cleaner to me than mixing the two different idioms.

        I didn't use sysread, just read, which uses buffering the same way as the diamond operator. It just reads a fixed number of bytes instead of scanning for EOL.

        You're right that sysread and syswrite would work fine; they might even be a little faster.