Php-mysql Course

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.status
var 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;
Output or Force Download MP3 with PHP

Last accessed pages

  1. Upload Script for Gallery of Images and Audio files (9766)
  2. Ajax-PHP File Manager (10352)
  3. CSS cursor property - Custom Cursors (6248)
  4. Create simple Website with PHP (44135)
  5. PHP Simple HTML DOM Parser (12478)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (348)
  2. CSS cursor property - Custom Cursors (41)
  3. The Mastery of Love (39)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (34)
  5. Read Excel file data in PHP - PhpExcelReader (31)