Solved

List files from multiple directories (php)

Posted on 2014-10-29
7
162 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
  • 4
  • 3
7 Comments
 
LVL 109

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 109

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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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 109

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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