Load MP3's from a folder with option to play on page

David Carr

Demos Tutorials PHP & MySQL

Table of Contents

Thus tutorial will show you how to grab videos from a folder and list them on a page, once listed an MP3 file can be clicked on and then played in a player or downloaded.

<iframe frameborder="0" height="315" src="//www.youtube.com/embed/ePgdnLkjZ9g" width="560"></iframe>

Player from http://www.alsacreations.fr/dewplayer-en


Download source files

Source Code

<?php ob_start();?>
<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>MP3 Test</title>


   $dirname = "music/";
   $files = scandir($dirname);
   $ignore = array(".", "..", ".DS_Store");

   echo '<ul>';
   foreach($files as $curfile){
       if(!in_array($curfile, $ignore)) {
           echo "<li><a href='?file=".$curfile."'>$curfile</a></li>n ";
   echo '</ul>'; 


    $file = $_GET['file'];

    echo '<object type="application/x-shockwave-flash" data="dewplayer.swf" width="200" height="20" id="dewplayer" name="dewplayer">
    <param name="movie" value="dewplayer.swf" />
    <param name="flashvars" value="mp3='.$dirname.$file.'" />
    <param name="wmode" value="transparent" />

    echo '<br /><a href="?download='.$file.'">Download</a>';



       $file = $dirname.$_GET['download'];
       header ("Content-type: octet/stream");
       header ("Content-disposition: attachment; filename=".$file.";");
       header("Content-Length: ".filesize($file));

<?php ob_flush(); ?>


Fathom Analytics $10 discount on your first invoice using this link

Help support the blog so that I can continue creating new content!


Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.