<?php
// wrapper script to serve user submitted mp3 files
ini_set("include_path""/path/to/pear:/secret/path");
require_once(
'httpStatus.inc');
define('CHUNK_SIZE'1024*1024);

function 
readfile_chunked($filename$retbytes TRUE) {
   
$buffer '';
   
$cnt =0;
    
// $handle = fopen($filename, 'rb');
   
$handle fopen($filename'rb');
   if (
$handle === false) {
      return 
false;
      }
   while (!
feof($handle)) {
      
$buffer fread($handleCHUNK_SIZE);
      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;
   }

$AUDIO_PATH="/somewhere/on/filesystem";
$thispage=$_SERVER['REQUEST_URI'];
$thisarray=explode("/",$thispage);
$n=(sizeof($thisarray) - 1);
$req_audio=$thisarray[$n];

if ( 
strcmp($req_audio,'rdb_audio.php') == ) {
   die();
   }

$fullpath $AUDIO_PATH "/" $req_audio;
if (
file_exists($fullpath)) {
   
$fi = new finfo(FILEINFO_MIME);
   
$mime $fi->buffer(file_get_contents($fullpath));
   
$foo explode(".",$req_audio);
   
$n=(sizeof($foo) - 1);
   
$type=$foo[$n];
   switch (
$mime) {
      case 
'audio/mpeg':
         
header("Content-type: audio/mpeg");
         break;
      case 
'application/ogg':
         
header("Content-type: audio/ogg");
         break;
      default:
         
http401NotFound('/vouchers/' $req_audio);
         die();
         break;
      }
            
header("Content-Transfer-Encoding: binary");
//   header('Accept-Ranges: bytes');
   
header('Content-length: ' filesize($fullpath));
         
header('Content-Disposition: inline; filename="' $req_audio '"');
         
header('X-Pad: avoid browser bug');
         
header('Cache-Control: no-cache');
   
readfile_chunked($fullpath);
//   readfile($fullpath);
   
} else {
   
http401NotFound('/vouchers/' $req_audio);
   }
?>