0

I'm trying to get images in a directory (with extensions jpg, jpeg, png, gif) using glob(). Here's my code:

$images = glob("uploads/*.{jpg,jpeg,png,gif}", GLOB_BRACE);

Because glob() is case sensitive, it's unable to find files with upper case and mixed case extensions. I want to find files with UPPERCASE (JPG, JPEG, PNG, GIF) and mixed case (JpG, jPeG, PNg, gIF) extensions.

I managed it to work using sql_regcase() on one extension, but it is now deprecated anyway.

$pattern = sql_regcase("jpg");
$images = glob("uploads/*.$pattern", GLOB_BRACE);

Is there any flexible solution for this problem?

Sai Kiran Sripada
  • 1,149
  • 3
  • 15
  • 30
  • @Michael Thanks for your quick response. But, none of those solutions work with multiple extensions. – Sai Kiran Sripada Jan 05 '14 at 01:46
  • 1
    Yours will be messier, but the same principle applies: `glob("uploads/*.{[jJ][pP][gG],[gG][iI][fF]}", GLOB_BRACE)` it'll work... – Michael Berkowski Jan 05 '14 at 01:47
  • This is working `glob("uploads/*.[jJ][pP][gG]", GLOB_BRACE)`. This is working too `glob("uploads/*.[gG][iI][fF]", GLOB_BRACE)`. This doesn't. `glob("uploads/*.{[jJ][pP][gG],[gG][iI][fF]}", GLOB_BRACE)`. It returns an empty array(). I couldn't make it work with multiple extensions. Where am I doing wrong? – Sai Kiran Sripada Jan 05 '14 at 02:10
  • 1
    Then use the `preg_grep('/\.(jpe?g|png|gif)$/i')` approach for filtering the `glob` list. – mario Jan 05 '14 at 02:18
  • 1
    What PHP version do you have? The exact same glob worked correctly for me testing on 5.5. That's why I suggested it. – Michael Berkowski Jan 05 '14 at 03:47
  • @MichaelBerkowski I'm running PHP 5.4, may be the syntax you've posted doesn't support in older versions. Thank you so much for your help! – Sai Kiran Sripada Jan 05 '14 at 04:36
  • @mario I used preg_grep() to make my code work. Thank you very much, bro. – Sai Kiran Sripada Jan 05 '14 at 04:37
  • @Kiran From 5.4 to 5.5 there are no documented changes.More likely, it could be a platform or underlying OS library difference. Mine worked on Linux, for example. In any case if `preg_grep()` works for you, that's sufficient. – Michael Berkowski Jan 05 '14 at 13:51
  • related: http://stackoverflow.com/questions/42461547/php-glob-with-case-insensitive-matching/42464571#42464571 – mickmackusa Feb 26 '17 at 04:02

0 Answers0