Symfony\Component\Finder\Finder::depth PHP Method

depth() public method

Usage: $finder->depth('> 1') // the Finder will start matching at level 1. $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
See also: DepthRangeFilterIterator
See also: NumberComparator
public depth ( string | integer $level ) : Finder | Symfony\Component\Finder\SplFileInfo[]
$level string | integer The depth level expression
return Finder | Symfony\Component\Finder\SplFileInfo[] The current Finder instance
    public function depth($level)
    {
        $this->depths[] = new Comparator\NumberComparator($level);

        return $this;
    }

Usage Example

 /**
  * @Route("/inbox", defaults={"_format"="json"})
  */
 public function dirAction(Request $request)
 {
     $dir = $request->query->get("dir", "");
     $type = $request->query->get("type", "file");
     $baseDir = realpath($this->container->getParameter('pumukit2.inbox'));
     /*
     if(0 !== strpos($dir, $baseDir)) {
         throw $this->createAccessDeniedException();
     }
     */
     $finder = new Finder();
     $res = array();
     if ("file" == $type) {
         $finder->depth('< 1')->followLinks()->in($dir);
         $finder->sortByName();
         foreach ($finder as $f) {
             $res[] = array('path' => $f->getRealpath(), 'relativepath' => $f->getRelativePathname(), 'is_file' => $f->isFile(), 'hash' => hash('md5', $f->getRealpath()), 'content' => false);
         }
     } else {
         $finder->depth('< 1')->directories()->followLinks()->in($dir);
         $finder->sortByName();
         foreach ($finder as $f) {
             if (0 !== count(glob("{$f}/*"))) {
                 $contentFinder = new Finder();
                 $contentFinder->files()->in($f->getRealpath());
                 $res[] = array('path' => $f->getRealpath(), 'relativepath' => $f->getRelativePathname(), 'is_file' => $f->isFile(), 'hash' => hash('md5', $f->getRealpath()), 'content' => $contentFinder->count());
             }
         }
     }
     return new JsonResponse($res);
 }
All Usage Examples Of Symfony\Component\Finder\Finder::depth