user1531040
user1531040

Reputation: 2301

How do you scan the directory case insensitive?

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)); ???

Upvotes: 1

Views: 1488

Answers (2)

shaack
shaack

Reputation: 181

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

$filenames = scandir($directory, SCANDIR_SORT_NONE);
natcasesort($filenames);

Upvotes: 1

Barmar
Barmar

Reputation: 781503

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);

Upvotes: 3

Related Questions