Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2012, 02:40 AM   PM User | #1
bobgodwin
New to the CF scene

 
Join Date: Jan 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
bobgodwin is an unknown quantity at this point
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..
bobgodwin is offline   Reply With Quote
Old 03-03-2012, 03:47 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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 />";
}
  
?>
mlseim is offline   Reply With Quote
Old 03-03-2012, 04:49 AM   PM User | #3
bobgodwin
New to the CF scene

 
Join Date: Jan 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
bobgodwin is an unknown quantity at this point
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
bobgodwin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:31 PM.


Advertisement
Log in to turn off these ads.