Simple file management with PHP

DEMO of the code, because this is what I always look for first whenever I read a code tutorial :)
Or just download the demo files.
Obligatory "use at your own risk" black-box warning

I love using MySQL for dealing with relational data as much as the next database admin (and if you have any doubts about the merits of MySQL, check out this article about an early decision by Google to migrate their AdWord data back to MySQL after trying out an expensive proprietary service and liking MySQL better). However, sometimes it’s just inconvenient to build an entire database model just to serve a discrete set of files in a directory on your server to a webpage.

For web-based file management, ajaxplorer is a fantastic tool for managing files on your server. However, without heavy modificaiton, it isn't really the type of thing you want to use to construct a webpage that visualizes the contents of a particular directory on your server.

I recently wanted to throw together a quick way to list the songs in the music directory on this website, Amidst the Panic. Because I was in a hurry (I am, after all, first and foremost a med student), I wanted to make it very easy to modify the names of the music documents, the order in which they are presented, and the music document itself if I wanted to switch to a new version. In MySQL, this could be done rather elegantly, but it requires a few extra steps. Instead, I decided to use PHP to scan a “music” directory and print a list of the songs in the directory. Then I used SoundManager 2 to play the songs as a playlist.

Here's how it works:

  • First, it scans the music directory for folders called "albums." each album is named as [YYYY]-[AlbumName]1
  • Each album contains a list of mp3 song files named [##]-[SongName].
  • The code prints an unordered list of albums in decending order (newest to oldest), with each list item containing a list of links to songs in that album
  • each album also contains a "tracklist.html" file that the script will print. You can use this as an easy way to provide custom view files for each album, or do something simple like just print an actual track list.
  • SoundManager 2 does the heavy lifting for turning the song links into a playable form on the page (in demo, not seen in generic listing code below)
  • To manage the list, simply add a new "album" folder and populate it with songs. If you use my regex matches, make sure to follow the naming conventions I described above.

You can view a demo of the output of the code below, and from there you can easily add on more complicated ways to present the information (I used a carousel on Amidst the Panic).

here is the code: (Note: code may vary slightly from the code presented in the working demo, but you can have a look for yourself if you download the demo file):

  //for presenting list of songs: 
echo '<ul class = "albumList">';
$albums = scandir('./music/'); //pick the directory to scan 

//now get the name of the albums: 
foreach($albums as $albumMatch) {
preg_match("/([a-z A-Z]+)/", $albumMatch, $matches);
$matches = array_unique($matches); //this ensures no doubles 

foreach ($matches as $album) { //"for each album" 
$fullAlbum = preg_replace('/([0-9]+)-([a-z A-Z]+)/', '$1', basename($albumMatch));

$noHidden = preg_match("/.[a-z A-Z]/", $fullAlbum);
//use $noHidden to make sure .DS_Store isn't listed

if (basename($album) == $noHidden) { 
   //self-explanatory variable declarations
   $tracklist = file_get_contents('music/'.$albumMatch .'/tracklist.html', true);
   $albumName = preg_replace('/([0-9]+?)-(.*)/', '$2', basename($album));
   $albumYear = preg_replace('/([0-9]+)-([a-z A-Z]+)/', '$1', basename($albumMatch));
  
   //"print the name of the album and the year"
   echo '<li><b>'.basename($album) . '</b><br />';
   echo $albumYear .'<br />';
   echo  $tracklist; //prints contents of album's tracklist.html file
			
   $songs = glob('music/'. $album. '/*.{mp3}', GLOB_BRACE);
   echo '<ol>';
  
  	//make a list item out of each song in an album
	foreach($songs as $song) {
  	$songName = preg_replace('/([0-9]+?)-(.*).mp3/', '$2', basename($song));
  	echo '<li>' . $songName . '</li>' ;
	} //end songs for loop
} // end 'if' statement for noHidden

echo '</ol>';
echo '</li>';
} //end matches (albums) forloop
} //end albums for loop 
echo '</ul>';

// fin

Published on March 5, 2013 in Projects

> > Simple file management with PHP