error in Recursive version of glob

Joined
Jun 30, 2005
Messages
59
Reaction score
0
Hi all . I got a php script that when i run it i get the following warnnings.
could any one help me fix these warnings .Thanks


Code:
Warning: Invalid argument supplied for foreach() in this line...

 foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir)
  {
   $aSubFiles = rglob($sSubDir, $sPattern, $nFlags);
   $aFiles = array_merge($aFiles, $aSubFiles);
  }

Code:
Warning: sort() expects parameter 1 to be array, boolean given in this line...

/* Get Listing of avatars */
$files = rglob (AVATARS_DIR.AVATARS_DIR_STOCK, '*');
sort ($files, SORT_STRING);
complete code:


Code:
<?php


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

/* Get Listing of avatars */
$files = rglob (AVATARS_DIR.AVATARS_DIR_STOCK, '*');
sort ($files, SORT_STRING);
 
Joined
Mar 17, 2005
Messages
159
Reaction score
0
where did you get the code from, the person who wrote it is your first port of call?

http://uk2.php.net/manual/en/function.glob.php
http://uk2.php.net/manual/en/control-structures.foreach.php

and any other php you are unsure about..

it's probably quite simple to fix - without looking through it an understanding fully whats there I wouldn't venture an exact fix - but generally I'd put some kind of conditional before the forloop,. either way first port of call should be the person(s) who wrote the code as they understand it :)

Sil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top