I want to stream an .wav audio file located in the application server directory, the file name should not be exposed to the end user. Im using Yii2 framework by the way, in general, how can I achieve this through PHP?
3 Answers
simple way. If you don't want to expose the file name then don't use name attribute.
<html>
<body>
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</body>
</html>
src is the path of your audio file and type is the type of the audio.
Comments
Actually I have managed to find a way to solve the problem.
Controller code
$audioFilePath = "audio_file_absolute_path.wav"
header('Cache-Control: no-cache');
header('Content-Transfer-Encoding: binary');
header('Content-Type: audio/wav'); // sets the output content type to wav
header('Content-Length: ' . filesize($audioFilePath)); // sets the legth of the file (in order to the HTML5 player to access the audio duration)
header('Accept-Ranges: bytes');
header('Content-Disposition: inline; filename="test_audio.wav"'); // set an output file name
readfile($audioFilePath); // reads the file
View code (The audio player)
<audio controls>
<source id="audioPlayerSource" src="<? 'Url of audio file read controller function' ?>" type="audio/wav">
</audio>
This way the audio file in the server gets streamed to the audio player in the view