Grow your CSS skills. Land your dream job.

PHP Problem – Dynamically loading images into a gallery

  • # February 4, 2013 at 9:17 pm

    Hi guys and gals!

    I’m using some PHP code to dynamically pull images from a directory on a static site with no problems.

    When I try to alter the code to pull images from a directory in a WordPress installation, it fails.

    < ?php

    $tempDir = get_bloginfo(‘template_url’) . ‘/img/gallery/generic/’;

    $myDirectory = opendir($tempDir);

    while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
    }

    closedir($myDirectory);
    $indexCount = count($dirArray);
    echo $indexCount;
    ?>

    The end echo statement is just so I can see whether it’s populating the array or not. There is another loop statement to display the images, which I believe is fine. I just can’t get my head around why this doesn’t seem to be working.

    Is anyone able to help?

    # February 4, 2013 at 11:23 pm

    What is the value of `$tempDir`?

    Does your call to `opendir()` succeed?

    You might also be interested in PHP’s [`glob()`](http://php.net/glob) function.

    # February 6, 2013 at 1:28 am

    Thanks for responding @traq.

    Umm… the first line is a $tempDir allocation, so that should be fine.
    I’m guessing it doesn’t succeed, as the array count is always 0. If I change $tempDir to a set path in a static site, the whole thing works fine. I think its the mixing of the template_url and relative path into a variable and then calling that variable that’s causing the issue, but I’m not certain.

    Had played around with glob() in the past, but once I got this working with opendir(), I moved on. Might have to go back and revisit.

    Any other ideas?

    # February 6, 2013 at 8:32 am

    As it turns out, for anyone who is stuck with a similar issue:

    Replacing get_bloginfo(‘template_url’) with TEMPLATEPATH fixes the issue.

    # February 6, 2013 at 10:17 am

    > the first line is a $tempDir allocation, so that should be fine.

    “should be,” sure. But you need to check. That function might be failing – you have no way to know unless you check.

    > Replacing get_bloginfo(‘template_url’) with TEMPLATEPATH fixes the issue.

    As a side note, you should avoid using URLs to access stuff on your own host. There’s no reason to open your front door, walk around the house, and come in through the back door just to get from the living room to your kitchen, right?

    :)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".