Skip to content

Instantly share code, notes, and snippets.

@fromheten
Created September 14, 2014 20:19
Show Gist options
  • Save fromheten/fa0df0af6514e66a0338 to your computer and use it in GitHub Desktop.
Save fromheten/fa0df0af6514e66a0338 to your computer and use it in GitHub Desktop.
<?php
$pathBase = '/var/mp3/Tales';
$urlBase = '/music/Tales';
// dirToArray
// $pathBase : A directory to decend down from
// $pathCurrent : this function meant to be run recursice, give an empty
string at innovactio.
// $urlBase : a component to prepend to objects.
function dirToArray( $pathBase, $pathCurrent, $urlBase) {
$contents = array();
foreach( scandir( $pathBase .'/'. $pathCurrent) as $node) {
$item = array();
# echo "DEBUG: Entering dirToArray loop for node
\"$node\".";
if( $node == '.' || $node == '..') continue;
if( is_dir( $pathBase . $pathCurrent . '/' . $node)) {
$contents = array_merge( $contents, dirToArray(
$pathBase, $pathCurrent . '/' . $node, $urlBase));
} else {
if( ! substr_compare( $node, ".m4b", -4, 4, 1) ) {
$item['m4b'] = $node;
$item['title'] = substr( $node, 0, -4);
$item['size'] = filesize( $pathBase .
$pathCurrent . '/' . $node);
# echo "DEBUG: Adding m4b item:
\"{$item['title']}\" \"$node\"\n";
// Ok, do we have a jpg file then?
foreach( scandir( $pathBase .'/'.
$pathCurrent) as $f) {
if( ! substr_compare( $f,
".jpg", -4, 4, 1) ) {
$item['jpg'] = $f;
}
}
}
}
if( array_key_exists( "m4b", $item)) {
$item['path'] = $urlBase . $pathCurrent;
$contents[] = $item;
}
}
return $contents;
}
// outputTableRows
// pathBase : A directory to decend down from
// di
function outputTableRows( $pathBase, $urlBase) {
# print_r( dirToArray( $pathBase, '', $urlBase));
foreach( dirToArray( $pathBase, '', $urlBase) as $item) {
#$url = "audiochef://192.168.1.4" . rawurlencode(
$item['path'] . "/" . $item['m4b']);
$url = "audiochef://192.168.1.4" . $item['path'] . "/" .
$item['m4b'];
echo "<li class=book>";
echo "<a href=\"$url\">" . htmlentities( $item['title'])
. "</a>";
#echo "</td><td>";
echo round( ($item['size'] / 1000 ), 0);
#echo "</td><td>";
if( array_key_exists( 'jpg', $item)) {
print <<<END
<img src="{$item['path']}/{$item['jpg']}"
style="width:150;height:150">
END;
}
print <<<END
</li>
END;
}
}
// Output page
#echo "<table border=\"1\" style=\"width:100%\">";
#outputTableRows( $pathBase, $urlBase);
#echo "</table>";
print <<<END
<style>
.book {
list-style-type: none;
height: 150px;
}
.book > * {
vertical-align: center;
height: 100%;
}
li:nth-child(odd) { background-color:tomato }
.book-list {
padding: 0
</style>
END;
echo "<ul class=book-list>";
outputTableRows( $pathBase, $urlBase);
echo "</ul>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment