Solved

List files from multiple directories (php)

Posted on 2014-10-29
7
165 Views
Last Modified: 2014-10-29
I have the following code and it works well, but I want to scan TWO (or more) instead of just one directory;

THIS WORKS FOR ONE-----


<?
$directory = "../../mpx/music/";                              
 
$songs = (glob($directory.'{*.mp3,*.m4a,*.ogg}', GLOB_BRACE));
       $x=0;
         
        foreach($songs as $song){
      echo '{';
          $x=$x+1;
          echo '"name": "'.basename($song).'",';
          echo '"url": "'.$song.'"';
        echo '},';
}
?>

Is there any thing I can do to change the ----- $directory = "../../mpx/music/";       -----
so that my script will list all the songs from multiple directories?

I tried this:

$directory = array(
          "../../mpx/music_dir01/",
          "../../mpx/music_dir02/"
     );

But Im a relative newbie.

Any advice would be appreciated - Thank You
0
Comment
Question by:edavo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40411841
Sure!  Use foreach() to iterate over the array of directory names.
http://us1.php.net/manual/en/control-structures.foreach.php
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40411884
This is untested code, but I think it is correct in principle.
<?php
error_reporting(E_ALL);

$dirs = array
( "../../mpx/music_dir01/"
, "../../mpx/music_dir02/"
)
;

foreach ($dirs as $directory)
{
    $songs = (glob($directory.'{*.mp3,*.m4a,*.ogg}', GLOB_BRACE));
    $x=0;
         
    foreach($songs as $song)
    {
        echo '{';
        $x=$x+1;
        echo '"name": "'.basename($song).'",';
        echo '"url": "'.$song.'"';
        echo '},';
    }
}

Open in new window

0
 

Author Comment

by:edavo
ID: 40412136
Follow up question.

I am using that same call twice - once at the top of the page to pull the list --- then once further below to output the urls to the songs.

It WORKS with the first query but not the second.

Is there a conflict by using it twice?
Should I change one of the $dirs or $directory variable names is the second use of the function?

Thank You - dM
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:edavo
ID: 40412158
I am close… I think Ill get it - if not I ping back in...
0
 

Author Comment

by:edavo
ID: 40412214
Got it - Thanks Ray!!!!!!!!! (Your a wizard)
0
 

Author Closing Comment

by:edavo
ID: 40412216
Quick reply, concise and on the money! Excellent!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40412347
Thanks, edavo!  Glad it's pointed in the right direction :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question