tags:

views:

37

answers:

3

hey guys im looking for a way to show all mp3 files in a directory

this is my code to get that :

    if ($handle = opendir($dirPath)) {

       while (false !== ($file = readdir($handle))) {

         if ($file = ".mp3" && $file = "..") {
             echo '
             <track>
              <location>'.$dirPath.$file.'</location>
              <creator>'.$file.'</creator>
            </track>
            ';    

          }
       }
   closedir($handle);
}

now i know that this script will only show mp3 files in parent directory , but i need to show all mp3 files in all directory inside parent directory

problem is this code cant show files inside sub directories !

A: 

You'll have to make a recursive function to search for all the MP3s.

Also, you probably meant if ($file == ".mp3" && $file == "..") { instead of if ($file = ".mp3" && $file = "..") {, and after that's changed, you get a condition that's always false. What are you trying to do there?

icktoofay
A: 

That code won't work at all. As you are setting the $file variable to ".." the result will be a lot of xml containing $dirPath and "..".

This is what you are looking for :)

$it = new RecursiveDirectoryIterator('path/to/files/');
foreach (new RecursiveIteratorIterator($it) as $file)
{
    echo $file->getPathname() . '<br />';
}
Decko
good point , but 1st how to apply filter mp3 files . 2nd the address $file->getPathname() this is not a web address but its like system path : britney\mylove\heart.mp3 and it would be useless path for web application !
Mac Taylor
@MacTaylor [Introduction to SPL](http://www.phpro.org/tutorials/Introduction-to-SPL.html) should have everything you need to know for now
Gordon
hey Gordon , the only problem cant fix , is getting web address . now the address is just like :britney\mylove\heart.mp3 and cant play the file . any point for that ?!
Mac Taylor
@MacTaylor see http://stackoverflow.com/questions/3156290/using-recursivedirectoryiterator-and-problem-in-path-name/3156351#3156351
Gordon
A: 

like icktoofay said, you'll have to make a recursive function. also, your code has an error:

if ($file = ".mp3" && $file = "..") {

won't work (and if ($file == ".mp3" && $file == "..") { is wrong, too). that line should look like this:

if (substr($file,-4) == ".mp3" || $file == "..") {

if you want to show the ".." - else it's just like this:

if (substr($file,-4) == ".mp3") {
oezi
my main goal is to find all files in a directory , cause i know how to find files in parent directory and i did that . thanks for your point
Mac Taylor