Solved

List files from multiple directories (php)

Posted on 2014-10-29
7
166 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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.
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…

624 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