Page 1 of 1

filter filenames in folder

Posted: Thu Nov 11, 2004 8:07 am
by rhosk
With the code below, I can manipulate to either list files that only has "sample" in the file name or list everything but "sample".

How can I apply 2 filters? I want to list everything except "sample" and "perm" in the filename. Thanks.

Code: Select all

<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder></image_folder><time>3</time><fade>1</fade><repeat>true</repeat><captions>false</captions></settings><images>';
create_tree("ssimages", '/^sample/');
function create_tree($file_dir, $filter = '') {
	global $xml;
	if ($handle = @opendir($file_dir))
{
	$list = array();
	while (false !== ($file = @readdir($handle))) { 
if ($file != "." && $file != "..") {
	if( !empty($filter) && !is_dir($file_dir . '/' . $file) )
//if( !preg_match($filter, $file) ) // get only "sample"
if( preg_match($filter, $file) ) //everything but sample
continue;
$list[] = $file;
}
}
foreach($list as $file) {
	if( is_dir($file_dir . '/' . $file) ) {
		create_tree($file_dir . "/" . $file, $filter);
		}
		else
		    $xml .= '<image>' . '<file>' . $file_dir . '/' . $file . '</file>' . '<caption>' . '</caption>' . '</image>' . "\n";
	}
	closedir($handle);
}
}
$xml .= '</images></slideshow>';
echo $xml;
?>

Posted: Thu Nov 11, 2004 10:17 am
by timvw

Code: Select all

preg_match("/$filter|$filter2/", $file)

Posted: Thu Nov 11, 2004 11:52 am
by rhosk
timvw wrote:

Code: Select all

preg_match("/$filter|$filter2/", $file)
Well, that didn't work. Please let me know if I'm close.

Code: Select all

<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder></image_folder><time>3</time><fade>1</fade><repeat>true</repeat><captions>false</captions></settings><images>';
create_tree("ssimages", '/^sample/', '/^perm/');
function create_tree($file_dir, $filter = '', $filter2 = '') {
global $xml;
if ($handle = @opendir($file_dir))
{
$list = array();
while (false !== ($file = @readdir($handle))) { 
if ($file != "." && $file != "..") {
if( !empty($filter) && !is_dir($file_dir . '/' . $file) )
if( preg_match("/$filter|$filter2/", $file) ) //everything but sample and perm
continue;
$list[] = $file;
}
}
foreach($list as $file) {
if( is_dir($file_dir . '/' . $file) ) {
create_tree($file_dir . "/" . $file, $filter, $filter2);
}
else
$xml .= '<image>' . '<file>' . $file_dir . '/' . $file . '</file>' . '<caption>' . '</caption>' . '</image>' . "\n";
}
closedir($handle);
}
}
$xml .= '</images></slideshow>';
echo $xml;
?>
This is still giving me every file in the folder

Posted: Thu Nov 11, 2004 4:41 pm
by timvw
rhosk wrote: Well, that didn't work. Please let me know if I'm close.
It works ;) But your code has other issues :p

Code: Select all

$filter1 = "hihi";
$filter2 = "bar";

$words = array();
$words[] = " i was walking";
$words[] = "he is hihi funny";
$words[] = "we had drinks at the bar";

foreach($words as $word)
{
    if (preg_match("/$filter1|$filter2/", $word))
    {
        echo "match: $word <br>";
    }
    else
    {
        echo "no match: $word <br>";
    }
}

Posted: Thu Nov 11, 2004 7:02 pm
by rhosk
This doesn't help me. Sorry, but I'm lost. I still can't get it to work.

Posted: Fri Nov 12, 2004 7:05 am
by timvw
change your code to: (so get rid of the / / )

Code: Select all

create_tree("ssimages", '^sample', '^perm');

Posted: Fri Nov 12, 2004 7:28 am
by rhosk
OMG, I can't believe it was that simple - geez. I see what you were trying to do for me and I appreiciate it. I do learn by trial and error, but mostly by getting it right and seeing WHY. Still learning this stuff.

Thanks for the tutor, Tim! And good luck with your 150 goal!! :)