Fellow Monks,
I have a piece of code to authenticate users and let them download files from a server. The files are in a folder hidden from public and are only downloaded by users with correct permission (username/password).

To prevent passing a real URL, I read the file and dump it to STDOUT, here's the code that does that:

sub dl_file { my $file = param('file'); $file =~ s/^\.+//; $file =~ s/^\///; if (-e "$folder/$file") { my $data; open IN, "$folder/$file" or die "Cannot open file $file, $!\ +n"; binmode IN; {local $/; $data = <IN>;} close IN; my $filename = (reverse (split /\//, $file))[0]; print header(-type=>"application/x-msdownload", -attachment=>$filename, -cookie=>[$cookie1, $cookie2]); print $data; } else { print $top, h1('Error'), p('file not found.'); print hr, a({-href=>"$me?command=MAIN"}, 'Back to Main'), $bot +tom; } }

However, one problem arised when the admin of the site placed a 12 Mb file there.. so when trying to download, the script attempts to read the whole 12Mb in and pass them as a response, so just waiting for the script to start responding results in a browser timeout.

I had the idea of issuing an exec "cat ", "$folder/$file" but couldn't cause they are using the script on an NT server.

What's the best solution to this? would doing a while loop and reading a specified buffer size solve this? or should I look into another way of doing it?


He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

In reply to Reading a big file and passing to output by Chady

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.