How to access the first result file Finder :: files ()?

How can I get the first file with symfony / finder ?

I tried to do like this:

<?php // ... $finder = new Finder(); $finder ->files() ->in($this->getKernel()->getRootDir().'/../web/uploads/') ->name($filename); if (!$finder->count()) { throw new NotFoundHttpException('Image not found'); } dump( $finder->count(), $finder->getIterator()->current(), $finder->getIterator()->valid() ); 

and I get this result:

 1 null false 
+6
source share
2 answers

Iterator Sequence:

  • rewind
  • valid
  • current / key
  • Following
  • valid
  • current / key
  • Following
  • ...

try \var_dump(\iterator_to_array($finder)); first or \var_dump(\iterator_to_array($finder));

+4
source
 $iterator = $finder->getIterator(); $iterator->rewind(); $firstFile = $iterator->current(); 
+7
source

Source: https://habr.com/ru/post/984315/


All Articles