I'd like to get a list of folders that don't contain certain files. I'm using this to list directories & files:

$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:

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:

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

Recommended Answers

All 3 Replies

Member Avatar for diafol

You could try this: I love glob but it doesn't have a negation. There are quite a few ways to do this, but this is a dirty and not very quick method. Have a look at SPL file - may suit you better. BTW - anybody who wants to tidy up my mess, feel free :)

function getExclusiveFiles($path,$f2e,$extensions=NULL,$pullpath=false){
	$ext = ($extensions) ? implode(',',$extensions) : '*';
	$dirs = glob($path . '*' , GLOB_ONLYDIR);
	$x=0;
	foreach($dirs as $dir){
		$holder = glob($dir . '/*.{' . $ext .'}', GLOB_BRACE);
		if(count($holder) && !array_intersect(preg_replace('/^(.*)/',$dir . '/$1',(array) $f2e),$holder)){
		  	$output[$x]=array('dir' => $dir,'files' => ($pullpath) ? preg_replace('/^(' . str_replace(array('.','/'),array('\.','\/'),$dir) . '\/)/','', $holder) : $holder);
			$x++;
		}	
	}
	return (isset($output)) ? $output : 'No directories found';
}

echo "<pre>";
	print_r(getExclusiveFiles('./',array('singleaudio.swf','singlevideo.swf'),array('mp3','mp4','flv','swf'),true));
echo "<pre>";

This is another solution:

<?php
$path = "media";
$dir_handle = @opendir($path) or die("Unable to open $path");

function list_dir($dir_handle, $path) {
    $result = array ();
    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");
            $files = list_dir($handle, $dir);
            if (!in_array('singlevideo.swf', $files)) {
            	echo "\n$file<br />\n";
            	foreach ($files as $file) {
            	    echo '- &nbsp;' . $file . '<br/>';
            	}
            }
        }
        elseif (preg_match('/(mp3|mp4|flv|swf)/', $file) && !in_array($file, $exclude)) {
            $result[] = $file;
        }
    }

    closedir($dir_handle);
    return $result;
}

list_dir($dir_handle,$path);
?>

Thanks for the replies. I ended up going with the the second example, as it was closer to what I had. Turns out I needed also to 1) Not show the other .SWFs and 2) Show only folders for another list. I ended up with this:

$path = "media";
$dir_handle = @opendir($path) or die("Unable to open $path");

function list_dir($dir_handle, $path) {
    $result = array ();
    while (false !== ($file = readdir($dir_handle))) {
        $players = array("audio.swf", "video.swf", "both.swf");
        $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");
            $files = list_dir($handle, $dir);
            if (!in_array('singlevideo.swf', $files) && !in_array('singleaudio.swf', $files)) {
            	echo ''.$file.'<br />'."\n";
            	foreach ($files as $file) {
            	    echo '&nbsp;&nbsp;&nbsp;&nbsp;' . $file . '<br/>'."\n";
            	}
            }
        }
        elseif (preg_match('/(swf|mp3|mp4|flv|mov)/', $file) && !in_array($file, $exclude) && !in_array($file, $players)) {
            $result[] = $file;
        }
    }
    closedir($dir_handle);
    return $result;
}
echo'
';
list_dir($dir_handle,$path);
echo'
';

Which gets rid of the unwanted folders and files. Taking out all but the .SWF extension in the preg_match show only the folders I need:

elseif (preg_match('/(swf)/', $file) && !in_array($file, $exclude) && !in_array($file, $players))]

Thanks.

commented: Thanks for sharing. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.