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 is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article><nav><ul>
<li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
<li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float.some_class {
width: 30%;
float: left;
}
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)var num = 12.34567;
num = Math.floor(num);
alert(num); // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr; // 30