What are some best practices for streaming audio directly from a website using PHP?

Streaming audio directly from a website using PHP involves sending the audio file in chunks to the client's browser to play it without fully downloading it. One way to achieve this is by using the readfile() function in PHP to read and output the audio file in chunks.

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

header('Content-Type: audio/mpeg');
header('Content-Length: ' . filesize($file));

$handle = fopen($file, 'rb');

while (!feof($handle)) {
    echo fread($handle, 8192);
    ob_flush();
    flush();
}

fclose($handle);
?>