The two code snippets presented in this page can be used to
output audio MP3 files, or to force the Download, with PHP.
-
Click on the code to select it.
Output MP3 with PHP
$mp3 ='path_to/mp3_file.mp3';
if(file_exists($mp3)) {
header('Content-Type: audio/mpeg');
header('Content-Disposition: inline; filename="mp3_file.mp3"');
header('Content-length: '. filesize($mp3));
header('Cache-Control: no-cache');
header('Content-Transfer-Encoding: chunked');
readfile($mp3);
exit;
}
Force Download MP3 file with PHP
$mp3 ='path_to/mp3_file.mp3';
if(file_exists($mp3)) {
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename="mp3_file.mp3"');
header('Content-length: '. filesize($mp3));
header('Cache-Control: no-cache');
header('Content-Transfer-Encoding: chunked');
readfile($mp3);
exit;
}
- Notice, the only difference is to this header():
header('Content-Disposition: attachment; filename="mp3_file.mp3"');
- "inline" to play the MP3 within the browser (if capabilities exist), and "attachment" to force download.
Daily Test with Code Example
HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas><embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line#id:first-line {
font-weight: bold;
color: blue;
}
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.statusvar url = window.location;
alert(url);
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()$homepage = file_get_contents("http://coursesweb.net/");
echo $homepage;