Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.43 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Readfile issue PHP
  2. if(file_exists($file_url)){header('Content-Type: '.$ftype);
  3. header('Content-Transfer-Encoding: binary');
  4. header('Content-Transfer-Encoding: binary');
  5. header('Expires: 0');
  6. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  7. header('Pragma: public');
  8. header("Content-Length: " . filesize($dir.$fname));
  9. header("Content-disposition: attachment; filename="".$fname.""");
  10. ob_clean();
  11. flush();
  12. readfile($dir.$fname);
  13. exit;}
  14.        
  15. $filename = $dir.$fname;
  16. $filesize = filesize($filename);
  17.  
  18.     $chunksize = 4096;
  19.     if($filesize > $chunksize)
  20.     {
  21.         $srcStream = fopen($filename, 'rb');
  22.         $dstStream = fopen('php://output', 'wb');
  23.  
  24.         $offset = 0;
  25.         while(!feof($srcStream)) {
  26.             $offset += stream_copy_to_stream($srcStream, $dstStream, $chunksize, $offset);
  27.         }
  28.  
  29.         fclose($dstStream);
  30.         fclose($srcStream);  
  31.     }
  32.     else
  33.     {
  34.         // stream_copy_to_stream behaves() strange when filesize > chunksize.
  35.         // Seems to never hit the EOF.
  36.         // On the other handside file_get_contents() is not scalable.
  37.         // Therefore we only use file_get_contents() on small files.
  38.         echo file_get_contents($filename);
  39.     }
  40.        
  41. session_name("download_".$filename);
  42. session_cache_limiter("public");
  43. session_start();
  44. header ("Content-Disposition: attachment; filename=".$filename."nn");
  45. header ("Content-Type: application/octet-stream");
  46. @readfile($link); // Path to file
  47.        
  48. function readfile_chunked($filename,$retbytes=true) {
  49.    $chunksize = 1*(1024*1024); // how many bytes per chunk
  50.    $buffer = '';
  51.    $cnt =0;
  52.    // $handle = fopen($filename, 'rb');
  53.    $handle = fopen($filename, 'rb');
  54.    if ($handle === false) {
  55.        return false;
  56.    }
  57.    while (!feof($handle)) {
  58.        $buffer = fread($handle, $chunksize);
  59.        echo $buffer;
  60.        ob_flush();
  61.        flush();
  62.        if ($retbytes) {
  63.            $cnt += strlen($buffer);
  64.        }
  65.    }
  66.        $status = fclose($handle);
  67.    if ($retbytes && $status) {
  68.        return $cnt; // return num. bytes delivered like readfile() does.
  69.    }
  70.    return $status;
  71. }
  72. header('Content-Type: '.$ftype);
  73. header('Content-Transfer-Encoding: binary');
  74. header('Expires: 0');
  75. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  76. header('Pragma: public');
  77. header("Content-Length: " . filesize($dir.$fname));
  78. header("Content-disposition: attachment; filename="".$fname.""");
  79. readfile_chunked($filen);