- Readfile issue PHP
- 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;}
- $filename = $dir.$fname;
- $filesize = filesize($filename);
- $chunksize = 4096;
- if($filesize > $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 > 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);
- }
- session_name("download_".$filename);
- session_cache_limiter("public");
- session_start();
- header ("Content-Disposition: attachment; filename=".$filename."nn");
- header ("Content-Type: application/octet-stream");
- @readfile($link); // Path to file
- function readfile_chunked($filename,$retbytes=true) {
- $chunksize = 1*(1024*1024); // how many bytes per chunk
- $buffer = '';
- $cnt =0;
- // $handle = fopen($filename, 'rb');
- $handle = fopen($filename, 'rb');
- if ($handle === false) {
- return false;
- }
- while (!feof($handle)) {
- $buffer = fread($handle, $chunksize);
- echo $buffer;
- ob_flush();
- flush();
- if ($retbytes) {
- $cnt += strlen($buffer);
- }
- }
- $status = fclose($handle);
- if ($retbytes && $status) {
- return $cnt; // return num. bytes delivered like readfile() does.
- }
- return $status;
- }
- header('Content-Type: '.$ftype);
- 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.""");
- readfile_chunked($filen);