The code below displays this:
/home/my_site/www/wp-content/uploads/2017/03/2017-03-17-my_file.mp3
As far as I can tell, this path is correct. Yet when I comment away the echo and download the file, it's an empty file. Why?
Code:
if (isset($_GET['file'])) { 
    clearstatcache();
    $file_path = str_replace('http://www.example.com/', '', $_GET['file']);
    $file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $file_path . '.mp3';
    echo $file_path;
    if(file_exists($file_path)) {
        $file_name = basename($file_path);
        $file_size = filesize($file_path);
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$file_size);
        header("Content-Disposition: attachment; filename=".$file_name);
        exit();
    }
    else {
        die('The provided file path is not valid.');
    }
}
EDIT: this is what I have after KIKO Software's suggestion. Still downloads empty file.
<?php
if (isset($_GET['file'])) { 
  $file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['file'] . '.mp3';
  //echo $file_path;
  //$file_path = $_SERVER['SCRIPT_FILENAME'];  
  if (file_exists($file_path)) {
    $file_name = basename($file_path);
    $file_size = filesize($file_path);
  header('Content-type: application/octet-stream');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: '.$file_size);
  header('Content-Disposition: attachment; filename='.$file_name);
  //readfile($file_path);
  exit();
  }
  else {
      die('The provided file path is not valid.');
  }
}
?>
 
     
    