How can I get the first file with symfony/finder component?
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
$iterator = $finder->getIterator();
$iterator->rewind();
$firstFile = $iterator->current();
Try to rewind
first or \var_dump(\iterator_to_array($finder));
.
The sequence of an iterator is:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With