jack_of_all_trades
jack_of_all_trades

Reputation: 332

php glob not returning all files

I looked at similar questions here and around the web, but none of the solutions have worked.

I'm using glob to return 177 images from a folder. Only some return. Sometimes nothing returns. Every time I reload the page, a few more images are loaded. In the inspector, everything looks like it's loaded; the entire code is how it should be. But looking at the page, I can see that there are obviously images missing.

Here's my code:

<?php

    set_time_limit(0);
    ignore_user_abort(1);

    $images = glob("images/pics/*", GLOB_BRACE);
    foreach ($images as $image) { ?>

    <div class='img_container_2 backing_center' style='background: none'>
        <img style='width: 400px' src = '<?php echo $image; ?>'/>
    </div>
<?php

    }

?>

I was thinking it might be a time-out problem, but setting the limit to 0 doesn't seem to make a difference. Any help is greatly appreciated!

Upvotes: 1

Views: 2913

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

PHP glob() function works perfectly in your code.

There is a problem with your directory:

  1. Be sure your folder contains images only
  2. Remove Thumbs.db in your $images array

Remove Thumbs.db

$key = array_search('Thumbs.db', $images );
$new_images = unset($images[$key]);

To Find Images only

$images = glob("images/pics/*.jpg", GLOB_BRACE);

Note : If your folder only contains images, then you don't have to use .jpg in glob function.

Upvotes: 1

jack_of_all_trades
jack_of_all_trades

Reputation: 332

My bad, I did a dumb thing. I set the width to an absolute value when it should have been set to a relative value. I'm still not sure why only some images were showing and some weren't, but at least it's fixed now. Thanks anyway for the help, everyone!

Upvotes: 0

Al Amin Chayan
Al Amin Chayan

Reputation: 2500

You specify GLOB_BRACE as the 2nd argument for glob in order for it to work.

So for example, if you executed glob("{a,b,c}.php", GLOB_BRACE) on the following list of files:

a.php

b.php

c.php 

Replace GLOB_BRACE with GLOB_NOSORT (Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically)

For more details see globe flags

Upvotes: 1

Related Questions