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 potential pitfalls of using direct variable insertion in SQL queries in PHP?
- What are the best practices for generating JavaScript using PHP to determine the width of the browser window?
- How can PHP developers effectively debug and troubleshoot code errors, especially related to data storage?