Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Excluding folders with certain files

    I,m using this function to list folders and files as a list:
    Code:
    function ListFolders($path) {
    	 //by Georgi Kralev
        $dir_handle = @opendir($path) or die("Unable to open $path");
        $dirname = end(explode("/", $path));
        echo ("<li>$dirname\n");
        echo "<ul>\n";
        while (false !== ($file = readdir($dir_handle))) {
            $exclude = array(".","..","_player_thumbs","Thumbs.db","audio.swf","both.swf");
            if(!in_array( $file, $exclude )) {
                if (is_dir($path."/".$file)) {
                    ListFolders($path."/".$file);
                }
                else {
    		if(preg_match('/(mp3|mp4|flv|swf)/', $file)) {
                    echo "<li>$file</li>";
    		}
                }
            }
        }
        echo "</ul>\n";
        echo "</li>\n";
       
        closedir($dir_handle);
    }
    ListFolders('media');
    Is there a way to exclude folders with a certain file in it?
    And a way to not list the parent directory?
    Last edited by bobgodwin; 03-03-2012 at 02:54 AM.

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,150
    Thanks
    10
    Thanked 1,163 Times in 1,154 Posts
    I'm not sure if what you're doing is recursive (multiple directory levels) or not.
    This method is a bit easier to do.

    This might get you started ... try this as a test script ...
    PHP Code:
    <?php

    // put only these types of files into an array.
    $files array_merge(
    glob("$path/*.mp3"),
    glob("$path/*.mp4"),
    glob("$path/*.flv"),
    glob("$path/*.swf")
    );

    // Display them all
    foreach($files as &$file){
    echo 
    "$file<br />";
    }
      
    ?>

  3. #3
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've tried glob. My else statement gives me the right files. Here's another script that I modified that doesn't show the parent directory:
    PHP Code:
    $path "media";
    $dir_handle = @opendir($path) or die("Unable to open $path");
    function 
    list_dir($dir_handle,$path) {
        while (
    false !== ($file readdir($dir_handle))) {
            
    $exclude = array(".","..","_player_thumbs","Thumbs.db");
            
    $dir =$path.'/'.$file;
            if(
    is_dir($dir) && !in_array($file$exclude)) {
                
    $handle = @opendir($dir) or die("undable to open file $file");
                echo 
    "\n$file<br />\n";
                
    list_dir($handle$dir);
            }elseif(
    preg_match('/(mp3|mp4|flv|swf)/'$file) && !in_array($file$exclude)) {
                echo 
    "&nbsp;&nbsp;&nbsp;&nbsp;$file<br />\n";
            }
        }
        
    closedir($dir_handle);
    }
    list_dir($dir_handle,$path); 
    This returns this:
    Code:
    All
        both.swf
        countdown1.flv
        countdown2.flv
        countdown3.flv
        countdown4.flv
        Lady in Red.mp3
        load.mp4
        load_1.mp4
        Mean Ol' Girl.mp3
        Story of My Life.mp3
        Summer's Snow.mp3
        Table for Two.mp3
        test_mp4_1.mp4
    Audio
        audio.swf
        Lady in Red.mp3
        Mean Ol' Girl.mp3
        Story of My Life.mp3
        Summer's Snow.mp3
        Table for Two.mp3
    One Song
        singleaudio.swf
        Story of My Life.mp3
    One Video
        singlevideo.swf
        test_mp4_1.mp4
    Video
        countdown1.flv
        countdown2.flv
        countdown3.flv
        countdown4.flv
        load.mp4
        load_1.mp4
        StatelyOneSong.swf
        test_mp4_1.mp4
        video.swf
    What I want is to leave out the directories with the "singleaudio.swf" & "singlevideo.swf" to get this:
    Code:
    All
        both.swf
        countdown1.flv
        countdown2.flv
        countdown3.flv
        countdown4.flv
        Lady in Red.mp3
        load.mp4
        load_1.mp4
        Mean Ol' Girl.mp3
        Story of My Life.mp3
        Summer's Snow.mp3
        Table for Two.mp3
        test_mp4_1.mp4
    Audio
        audio.swf
        Lady in Red.mp3
        Mean Ol' Girl.mp3
        Story of My Life.mp3
        Summer's Snow.mp3
        Table for Two.mp3
    Video
        countdown1.flv
        countdown2.flv
        countdown3.flv
        countdown4.flv
        load.mp4
        load_1.mp4
        StatelyOneSong.swf
        test_mp4_1.mp4
        video.swf


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •