Here's what I came up with
Given
nick@avari ~/code/php $ ls -l -R
.:
total 12
drwxr-xr-x 2 nick nick 4096 Jul 13 13:27 audio
drwxr-xr-x 2 nick nick 4096 Jul 13 13:27 images
-rw-r--r-- 1 nick nick 1166 Jul 13 14:02 test.php
./audio:
total 0
-rw-r--r-- 1 nick nick 0 Jul 13 13:27 lol.mp3
./images:
total 0
-rw-r--r-- 1 nick nick 0 Jul 13 13:27 lol.png
Output:
<a class='myPlayer' href='audio/lol.mp3' style='background-image:url(images/lol.png)'>
</a>
And... here's the code
<?php
function directory_to_array($dir) {
$ret = array();
$dh = opendir($dir);
while (($file=readdir($dh)) !== false) {
if ($file != "." && $file != "..") // ignore . and ..
$ret[] = $file;
}
closedir($dh);
return $ret;
}
function match_files($audio, $image) {
//assume basename in $audio and $image are the same
//merges the two to a new array
$ret = array();
foreach ($audio as $name) {
$abase = explode(".", $name);
foreach ($image as $img) {
$ibase = explode(".", $img);
if ($abase[0] == $ibase[0]) {
$ret[] = array($name, $img);
}
}
}
return $ret;
}
function merge_to_player($input) {
foreach ($input as $pair) {
print "<a class='myPlayer' href='audio/$pair[0]' style='background-image:url(images/$pair[1])'>
</a>
";
}
}
//get the audio directory contents
$audio = directory_to_array("audio");
//get the image directory contents
$image = directory_to_array("images");
$matched = match_files($audio, $image);
merge_to_player($matched);
?>