Search code examples
phpsymfony1

List all files in a directory


using symfony I'm trying to create a page listing all the images in a web folder

I created the following action :

$dir = 'images/blog';
$fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir";
$d = @dir($fulldir) or die('Failed opening directory for reading');

while(false !== ($entry = @$d->read()))
{
    $this->imagesBlog[] = array(
        "file" => "/$dir/$entry",
        "size" => getimagesize("$fulldir/$entry"));

    $d->close();
}

And the following template :

foreach($imagesBlog as $img)
    echo '<img class="photo" src="'.$img['file'].'" ' . $img['size'][3].'>'."\n";

This seems to work, but returns only one image from a folder containing multiple files.

print_r($imagesBlog):

sfOutputEscaperArrayDecorator Object
(
    [count:sfOutputEscaperArrayDecorator:private] => 1
    [value:protected] => Array
        (
            [0] => Array
                (
                    [file] => /images/blog/FM-stupidest.png
                    [size] => Array
                        (
                            [0] => 300
                            [1] => 252
                            [2] => 3
                            [3] => width="300" height="252"
                            [bits] => 8
                            [mime] => image/png
                        )

                )

        )

    [escapingMethod:protected] => esc_specialchars
)

Help ! I'm loosing my mind here.


Solution

  • Wouldn't it be better to call $d->close(); outside the while loop?

    I think this is the reason - after finding the first image, the resource will be closed and the next read() will fail.