Creating/printing array issues

Creating/printing array issues

am 28.07.2009 20:06:23 von Allen McCabe

--0016364ef507b16005046fc7ed1b
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

I found a php script to find all file types with a file-name formula and put
them into an array. I then wanted to echo this array (possibly with links to
the files).

I want my default page to look for and list all php files with
"survey_*.php", the asterisk being any number, so the results should be:
survey_01.php, survey_02.php, etc.

Here is my code:

/**
* Recursive version of glob
*
* @return array containing all pattern-matched files.
*
* @param string $sDir Directory to start with.
* @param string $sPattern Pattern to glob for.
* @param int $nFlags Flags sent to glob.
*/
function rglob($sDir, $sPattern, $nFlags = NULL)
{
$sDir = escapeshellcmd($sDir);
// Get the list of all matching files currently in the
// directory.
$aFiles = glob("$sDir/$sPattern", $nFlags);
// Then get a list of all directories in this directory, and
// run ourselves on the resulting array. This is the
// recursion step, which will not execute if there are no
// directories.
foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir)
{
$aSubFiles = rglob($sSubDir, $sPattern, $nFlags);
$aFiles = array_merge($aFiles, $aSubFiles);
}
// The array we return contains the files we found, and the
// files all of our children found.
return $aFiles;
}
$aryPhotos = rglob("./surveys","\\{survey_*.php}",GLOB_BRACE
);
$aryExt = array("php");
$propid = $_REQUEST['mlsid']; // I assume the id will be in the querystring
// get the proper match pattern according to our Extension criteria
foreach ($aryExt as $e) {
$aryPattern[] = $propid."_*.$e";
}
$pattern = join(",", $aryPattern); // comma separated list
// Get the filenames
$aryPhotos = rglob("./surveys","\\{$pattern}",GLOB_BRACE);

//print out our array
print_r($aryPhotos);


?>


Also, it would be nice to have an if/else statement to say there are no
"surveys" (survey_*.php files) available if there are none in the /surveys
directory.

Any and all help would be greatly appreciated!

-Allen McCabe

--0016364ef507b16005046fc7ed1b--

Re: Creating/printing array issues

am 28.07.2009 20:09:24 von Allen McCabe

--0016361640ad78cb70046fc7f89b
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

By the way, I was getting a PHP error with an unexpected ")" on line 47
($aryPhotos), so I added the two \\ before {survey*_.php and now the page
won't load, won't even display a PHP error.



On Tue, Jul 28, 2009 at 11:06 AM, Allen McCabe wrote:

> I found a php script to find all file types with a file-name formula and
> put them into an array. I then wanted to echo this array (possibly with
> links to the files).
>
> I want my default page to look for and list all php files with
> "survey_*.php", the asterisk being any number, so the results should be:
> survey_01.php, survey_02.php, etc.
>
> Here is my code:
>
> > /**
> * Recursive version of glob
> *
> * @return array containing all pattern-matched files.
> *
> * @param string $sDir Directory to start with.
> * @param string $sPattern Pattern to glob for.
> * @param int $nFlags Flags sent to glob.
> */
> function rglob($sDir, $sPattern, $nFlags = NULL)
> {
> $sDir = escapeshellcmd($sDir);
> // Get the list of all matching files currently in the
> // directory.
> $aFiles = glob("$sDir/$sPattern", $nFlags);
> // Then get a list of all directories in this directory, and
> // run ourselves on the resulting array. This is the
> // recursion step, which will not execute if there are no
> // directories.
> foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir)
> {
> $aSubFiles = rglob($sSubDir, $sPattern, $nFlags);
> $aFiles = array_merge($aFiles, $aSubFiles);
> }
> // The array we return contains the files we found, and the
> // files all of our children found.
> return $aFiles;
> }
> $aryPhotos = rglob("./surveys","\\{survey_*.php}",GLOB_BRACE);
> $aryExt = array("php");
> $propid = $_REQUEST['mlsid']; // I assume the id will be in the querystring
>
> // get the proper match pattern according to our Extension criteria
> foreach ($aryExt as $e) {
> $aryPattern[] = $propid."_*.$e";
> }
> $pattern = join(",", $aryPattern); // comma separated list
> // Get the filenames
> $aryPhotos = rglob("./surveys","\\{$pattern}",GLOB_BRACE);
> //print out our array
> print_r($aryPhotos);
>
>
> ?>
>
>
> Also, it would be nice to have an if/else statement to say there are no
> "surveys" (survey_*.php files) available if there are none in the /surveys
> directory.
>
> Any and all help would be greatly appreciated!
>
> -Allen McCabe
>

--0016361640ad78cb70046fc7f89b--