Ashley Merrick
06-02-2008, 03:38 PM
I have a Hard Drive which has quite alot of songs, I want to be able to create a PHP page that will display all the songs on the hard drive. Currently I have found and modded a script, however it only looks in one folder currently. I need the script to dig through all the subdirectories and show them music files aswell.
<?php
$path = "E:\Alter Bridge\Blackbird [UK-OZ-NZ Version]";
$dir_handle = @opendir($path) or die("Unable to open $path");
while (false !== ($file = readdir($dir_handle))) {
if(strstr($file,"." ) == ".wma.jpg")
continue;
echo "<a href=\"$file\">$file</a><br />";
}
closedir($dir_handle);
?>
I want to be able to set $path to "E:\" and it go through every folder in that drive and scan for music files.
Thanks,
Ashley
epcmedia
06-02-2008, 05:41 PM
Hello,
I'm working on a project which does this. Here is one solution:
look into the php glob() function
http://php.net/glob
solution taken from here:
http://us2.php.net/manual/en/function.glob.php#51582
(http://us2.php.net/manual/en/function.glob.php#51582)
Your code:
$path = "E:\Alter Bridge\Blackbird [UK-OZ-NZ Version]";
$dir_handle = @opendir($path) or die("Unable to open $path");
while (false !== ($file = readdir($dir_handle))) {
if(strstr($file,"." ) == ".wma.jpg")
continue;
echo "<a href=\"$file\">$file</a><br />";
}
closedir($dir_handle);
Glob code which works recursively:
function globr($sDir, $sPattern, $nFlags = NULL) {
$aFiles = glob("$sDir/$sPattern", $nFlags);
$files = getDir($sDir);
if (is_array($files)) {
foreach( $files as $file ) {
$aSubFiles = globr($file, $sPattern, $nFlags);
$aFiles = array_merge($aFiles,$aSubFiles);
}
}
return $aFiles;
}
function getDir($sDir) {
$i=0;
if(is_dir($sDir)) {
if($rContents = opendir($sDir)) {
while($sNode = readdir($rContents)) {
if(is_dir($sDir.'/'.$sNode )) {
if($sNode !="." && $sNode !="..") {
$aDirs[$i] = $sDir.'/'.$sNode ;
$i++;
}
}
}
}
}
return $aDirs;
}
USAGE: globr('E:\Alter Bridge\Blackbird [UK-OZ-NZ Version]','{*.wma,*.jpg}',GLOB_BRACE)
Hope that helps.
Ashley Merrick
06-02-2008, 06:13 PM
<?php
function globr($sDir, $sPattern, $nFlags = NULL) {
$aFiles = glob("$sDir/$sPattern", $nFlags);
$files = getDir($sDir);
if (is_array($files)) {
foreach( $files as $file ) {
$aSubFiles = globr($file, $sPattern, $nFlags);
$aFiles = array_merge($aFiles,$aSubFiles);
}
}
return $aFiles;
}
function getDir($sDir) {
$i=0;
if(is_dir($sDir)) {
if($rContents = opendir($sDir)) {
while($sNode = readdir($rContents)) {
if(is_dir($sDir.'/'.$sNode )) {
if($sNode !="." && $sNode !="..") {
$aDirs[$i] = $sDir.'/'.$sNode ;
$i++;
}
}
}
}
}
return $aDirs;
}
globr('E:\Alter Bridge\Blackbird [UK-OZ-NZ Version]','{*.wma,*.jpg}',GLOB_BRACE);
?>Returns Notice: Undefined variable: aDirs in C:\Program Files\EasyPHP 2.0b1\www\index.php on line 28
Or am I just half asleep and missing something?
Thanks,
Ashley
epcmedia
06-02-2008, 06:51 PM
hmmm... i got no error. you can try this:
define aDris like so:
function getDir($sDir) {
$i=0;
$aDirs = array();
if(is_dir($sDir)) {