10

I'm trying to use PHP's Glob to get a list of files based on a wildcard, namely the extension.

$images = glob('/content/big/'.$item['id'].'.{jpg,jpeg,png,gif}', GLOB_BRACE);

I know there is a file in this directory, namely: 23.png but it doesn't show in array $images. I don't have a clue why not. I've tried making the URL even more absolute (or explicit) like:

$images = glob('http://www.website.com/content/big/'.$item['id'].'.{jpg,jpeg,png,gif}', GLOB_BRACE);

Without result.

Could it be that Glob isn't installed properly inside PHP? Or is there another reason this doesn't give any results?

tvgemert
  • 1,436
  • 3
  • 25
  • 50
  • 1
    is there an absolute path `/content/big` on that server? Note that your path means server root, not document root. And urls dont work in glob as noted in the Notes section on the manual page. – Gordon Aug 09 '11 at 14:38
  • As stated below, I did follow that path but got a bit lost after all. – tvgemert Aug 09 '11 at 15:31

2 Answers2

16

glob only works with paths on the server's file system, not URLs.

http://www.website.com/content/big/ may really be /var/www/site/content/big on the server, and that's the path you need to use.

Staring a path with a / makes glob look in your root for that folder, and I'm assuming there is no folder called /content/big/ on your server.

Try it like this (using a relative path from the server root):

$images = glob('content/big/'.$item['id'].'.{jpg,jpeg,png,gif}', GLOB_BRACE);

Or use an absolute path:

$images = glob('/var/www/site/content/big/'.$item['id'].'.{jpg,jpeg,png,gif}', GLOB_BRACE);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Thank you for your clear explanation, that should be the solution and makes all the sense in the world. (Thus marked your answer as THE answer. Although I did try that already) But it doesn't seem to be the solution in my case, still no results in $images. Could there be something wrong with "my" Glob function??? I'm going to poke around some more, hope to find out where I (or something else ;) screwed up. – tvgemert Aug 09 '11 at 15:29
  • 1
    @tvgemert: Glad I could help. I'd say double check your folder, make sure the files really are in there. Also, try giving `glob` other folders to make sure it does give you results. Good luck :-) – gen_Eric Aug 09 '11 at 15:37
1

below is my implementation, single quotes did not work with the echo, but this works for me. Hope it helps!

            <ul>
                    <?php
                            foreach(glob('audio/*.mp3') as $audio){ echo "<li><a>$audio</a></li>";}
                    ?>
            </ul>
hinekyle
  • 822
  • 8
  • 16