Search code examples
phphtmlmp3

Hide audio url in PHP


I am playing audio in mp3 format but I want to hide the mp3 url.
I'm using PHP and I try this code but his is not working.
HTML

  <audio controls>
   <source src="mp3.php?id=1" type="audio/mpeg">
    Your browser does not support the audio element.
  </audio>   

PHP

 <?php
    $id = $_GET['id'];

  if($id == 1){
     echo '/path/to/audio.mp3';
  }

 ?>

What is wrong with this code any suggestion.
How can I do that.
Thanks...


Solution

  • Not tried, but this might work:

    $filename = '/path/to/audio.mp3';
    if(is_file($filename)) 
    {
        header('Content-Type: audio/mpeg');
        header('Content-Disposition: inline;filename="'.basename($filename).'"');
        header('Content-length: '.filesize($filename));
        header('Cache-Control: no-cache');
        header("Content-Transfer-Encoding: chunked"); 
        readfile($filename);
    }
    

    Basically we are outputting the raw audio stream in the location where mp3.php is called. This is also how we do it when we say <img src='img.php'>