...

View Full Version : Excluding folders with certain files



bobgodwin
03-03-2012, 02:40 AM
I,m using this function to list folders and files as a list:

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?

mlseim
03-03-2012, 03:47 AM
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

// 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 />";
}

?>

bobgodwin
03-03-2012, 04:49 AM
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:

$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum