...

View Full Version : Problems w/ code I have to change that lists files in a dir



musher
02-16-2006, 11:13 PM
I'm having some problems understanding and change some code I have to maintain that creates a list of files in a folder.

This was orgininaly set up for a single folder with no sub folders I've added sub folders but do not want them listed in the displayed table of files, just a list of files (or thumbnails as is the case).

below are the two functions that build the table (file/directory) list, part of what's got me confused is the preg_match line in the GetDirList function I think thats stripping out . and .. but not sure (I've read and read docs but it isn't sinkin in)

if some one could explain that to me and also point me in the right direction of striping out the folder names I'd be most gratefull.



function BuildImageTable($Directory) {
$p_Files = GetDirList($Directory);
$sTableFormat = "<table border=\"3\" cellspacing=\"0\" cellpadding=\"20\">{ROWS}</table>";
$sRows = "";
$NumCols = 3;
for($iRow = 0; $iRow <= count($p_Files) / $NumCols; $iRow++) {
$sRows .= "<tr>";
for($iCol = 0; $iCol < $NumCols; $iCol++) {
$iElem = $iRow * $NumCols + $iCol;
if($iElem >= count($p_Files)) {
break;
}else{
$sRows .= "<td valign=bottom><center><a CLASS=\"h3\" HREF=\"$Directory/{$p_Files[$iElem]}\" TARGET=\"_blank\"><IMG SRC=\"$Directory/Thumbnails/{$p_Files[$iElem]}\" BORDER=\"0\"></a></center>
<center>{$p_Files[$iElem]}</center><center><a CLASS=\"h3\" HREF=\"Upload.php?value={$p_Files[$iElem]}&dir=$Directory\" TARGET=\"_top\"> Delete</a></center>";
}
}
$sRows .= "</tr>";
}
return str_replace("{ROWS}", $sRows, $sTableFormat);
}




function GetDirList($dir) {
$dl = array();
if ($hd = opendir($dir)) {
while ($sz = readdir($hd)) {
if (preg_match("/^\./",$sz)==0) {
$dl[] = $sz;
}
}
closedir($hd);
}
asort($dl);
return $dl;
}

fci
02-17-2006, 12:18 AM
you most likely want to ignore . .. but that regex ignores anything beginning with a period (which may or may not be desired).

I would change it to:

while ($sz = readdir($hd)) {
if (preg_match("/^\.\.?$/",$sz) || is_dir($sz))
continue;
$dl[] = $sz;
}

musher
02-17-2006, 04:30 AM
you most likely want to ignore . .. but that regex ignores anything beginning with a period (which may or may not be desired).

I would change it to:

while ($sz = readdir($hd)) {
if (preg_match("/^\.\.?$/",$sz) || is_dir($sz))
continue;
$dl[] = $sz;
}


Thanks fci, I kind of understand what you mean about it would ignores anything beginning with a period (which could be bad).

Still don't really understand what ("/^\.\.?$/",$sz) is doing/means :confused:
And still not sure how to stop folders/directories from being displayed

fci
02-17-2006, 06:22 AM
^ => beginning of string
\. => a period
\.? => a period that is optional
$ => end of string

in english:
a string that begins with a period and optionally has another period, then the string will end.
which matches:
. or ..

I added is_dir($sz) which meant it would continue and not add a dir to the array.

musher
02-17-2006, 04:55 PM
fci,
Thanks for the help and thanks big time for the explaning

your my new hero :D

actualy fond a problem with the snipit (stuff occasionaly sinks in), I hope I got this right it seams to work ok.

1. found out didnt have to check for . or .. when using is_dir()
2. must include path with is_dir()



while ($sz = readdir($hd)) {
if (is_dir($dir.$sz))
continue;
$dl[] = $sz;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum