<?php

function readfile_chunked ($filename) { 
  $chunksize = 1*(1024*1024); // how many bytes per chunk 
  $buffer = ''; 
  $handle = fopen($filename, 'rb'); 
  if ($handle === false) { 
    return false; 
  } 
  while (!feof($handle)) { 
    $buffer = fread($handle, $chunksize); 
    print $buffer; 
  } 
  return fclose($handle); 
} 



///////// Script Main Code /////////


$param_URI = str_replace('/1.0/firmwares','',$_SERVER['REQUEST_URI']);

$i=1;
foreach ( explode('/',$param_URI) as $pair ) {
$param[$i] = $pair;
$i++;
}

    $yourfile = $_SERVER["DOCUMENT_ROOT"].'/downloads/'.end($param);

    $file_name = basename($yourfile);

    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Content-Length: " . filesize($yourfile));
//    header("Connection:  keep-alive");

    readfile_chunked($yourfile);


 $countFilePath = $_SERVER["DOCUMENT_ROOT"].'/downloads/count_'.$file_name.'.txt';
 $count = file_exists($countFilePath) ? file_get_contents($countFilePath) : 0;
 file_put_contents($countFilePath, ++$count);

 echo $count;

?>