That simple?

gvim gvimrc at gmail.com
Tue Apr 24 13:40:50 BST 2012


I'm trying to serve a large zip file (678MB) in 3MB chunks to which I will add password protection. I found the PHP code listed below but wanted to stick with Perl. My first, potentially naive, solution is listed below but I want to make sure I'm not overlooking some essential checking and edge cases.

givm

************ Perl ********************

use CGI::Simple::Standard qw( header );

open my $INFILE, '<', 'dld.zip' or die "Can't open file: $!\n";
print header(-type => 'application/zip', -attachment => 'dld.zip');
while (read $INFILE, my $buffer, 3145728) {
   print $buffer;
}
close $INFILE;

************** PHP *******************

   function readfile_chunked($filename) {
     $buffer = '';
     $handle = fopen($filename, 'rb');
     if ($handle === false) {
       return false;
     }
     while (!feof($handle)) {
       $buffer = fread($handle, 3145728);
       echo $buffer;
       ob_flush();
       flush();
     }
     $status = fclose($handle);
     return $status;
   }

    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename = "facial_exercises.zip"');
    readfile_chunked('dld.zip');



More information about the london.pm mailing list