Solved

List files from multiple directories (php)

Posted on 2014-10-29
7
163 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display images from mysql blob type (Not working) 9 41
PHP not parsing ' character 12 46
Uploading a CSV Data Import via PHP & MySql 3 29
Log in through ID 5 17
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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

856 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