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);
?>
Keywords
Related Questions
- What are the advantages of using a database over storing data in text files in PHP?
- How can PHP developers efficiently handle sessions on every page without repeating the session_start() command?
- What are the potential pitfalls of using str_replace() in PHP for character substitutions, and how can they be avoided?