1

I want to get files from a directory. And I don't if it is uppercase or lowercase. How do I scan the directory case insensitive in php?

strtolower(scandir($directory, SCANDIR_SORT_ASCENDING)); ???
Barmar
  • 741,623
  • 53
  • 500
  • 612
user1531040
  • 2,143
  • 6
  • 28
  • 48

2 Answers2

3

scandir() returns an array, strtolower() only works on strings. You need to use array_map() to run the function on all the names returned:

$filenames = array_map('strtolower', scandir($directory));

Since the sorting done by scandir is case-sensitive, you should sort $filenames after you retrieve them.

sort($filenames);
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

This one preserves the upper and lower case in the array but sorts it case insensitive.

$filenames = scandir($directory, SCANDIR_SORT_NONE);
natcasesort($filenames);
shaack
  • 171
  • 8