How can PHP be used to optimize the delivery of audio files for streaming or downloading on a website?

To optimize the delivery of audio files for streaming or downloading on a website, you can use PHP to set appropriate headers for the audio files. By setting the correct headers, you can control how the browser handles the audio file, such as streaming it instead of waiting for the full file to download before playing.

<?php
$file = 'audio.mp3';

header('Content-Description: File Transfer');
header('Content-Type: audio/mpeg');
header('Content-Disposition: inline; filename=' . $file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

readfile($file);
?>