YASB – Yet Another Symfony Blog

June 21, 2008

A Symfony Quickie: Finding a File

Filed under: General, Tips and tricks — Krof Drakula @ 8:10 pm

It’s been a while, but I’ve finally made the plunge and did the first in the series of cookbook-related screencast tutorials for Symfony.


Edit: here’s the gist of the video.

== The sfFinder class ==

Symfony comes bundled with the sfFinder class that lets you conveniently search for files and directories. It has a simple fluent interface that lets you tell what you’re looking for and where to look for it:


To illustrate this example, let’s use an action that lists the contents of the upload folder. Inside an action, we assign the array returned by sfFinder to the template:

$this->images = sfFinder::type('file')->name('*.jpg')->relative()->in(sfConfig::get('sf_upload_dir'));

Since, by default, sfFinder returns absolute paths, we tell it to return everything relative to the search directory – this will include subdirectories as well.

In a template, we can now iterate over the images to list them:

foreach($images as $image):
  echo '<li>' . image_tag('/uploads/' . $image); . '</li>';

Powered by WordPress