kode
kode

Reputation: 1078

How to read huge file and output it with php

I want to handle big files(1-2Gb) downloads with php script and found two ways to do that:

  1. with file_get_contents()
  2. with readfile()

and use this implementation:

header('Content-type: ' . $string);
header('Content-disposition: attachment; filename=' . $info['filename']);
$file = file_get_contents($filename);
echo $file;
or
readfile($filename);

But it takes too long to output the file. I suppose that the whole file has to be readed, before the output starts. It is more quickly when point the exact location of the file. Then it starts the output almost immediately.

I am looking for solution that streams the file or something. Any ideas?

Upvotes: 4

Views: 2303

Answers (2)

Johan van der Slikke
Johan van der Slikke

Reputation: 765

$handle = fopen($this->_path, 'rb');

while(!feof($handle))
{
    echo fread($handle, 4096);

    ob_flush();
    flush();    
}

fclose($handle);

from PHP Readfile() not working for me and I don't know why

Upvotes: 3

Jani Hartikainen
Jani Hartikainen

Reputation: 43243

You should consider using mod_xsendfile

Upvotes: 6

Related Questions