Readfile issue PHP
<p>I am trying to force a download using php headers and the readfile function, here is my code:</p>

<pre><code>if(file_exists($file_url)){header('Content-Type: '.$ftype);
header('Content-Transfer-Encoding: binary');
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public');
header("Content-Length: " . filesize($dir.$fname));
header("Content-disposition: attachment; filename=\"".$fname."\""); 
ob_clean(); 
flush(); 
readfile($dir.$fname);
exit;}
</code></pre>

<p>The issue is that files greater then 37mb are downloading at 0 bytes. I have checked the php config and <code>memory_limit</code> is set to 200mb, I have tried downloading in chunks with this:</p>

<pre><code>$filename = $dir.$fname;
$filesize = filesize($filename);

    $chunksize = 4096;
    if($filesize &gt; $chunksize)
    {
        $srcStream = fopen($filename, 'rb');
        $dstStream = fopen('php://output', 'wb');

        $offset = 0;
        while(!feof($srcStream)) {
            $offset += stream_copy_to_stream($srcStream, $dstStream, $chunksize, $offset);
        }

        fclose($dstStream);
        fclose($srcStream);   
    }
    else 
    {
        // stream_copy_to_stream behaves() strange when filesize &gt; chunksize.
        // Seems to never hit the EOF.
        // On the other handside file_get_contents() is not scalable. 
        // Therefore we only use file_get_contents() on small files.
        echo file_get_contents($filename);
    }
</code></pre>

<p>But instead of forcing the download it displays the file. Any ideas?</p>
