Solved

List files from multiple directories (php)

Posted on 2014-10-29
7
160 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 108

Expert Comment

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

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Author Comment

by:edavo
Comment Utility
Got it - Thanks Ray!!!!!!!!! (Your a wizard)
0
 

Author Closing Comment

by:edavo
Comment Utility
Quick reply, concise and on the money! Excellent!
0
 
LVL 108

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now