Php – How to make is_dir($FoLdEr) case insensitive in php

case-insensitive, php

I need is_dir() to don't care about if the folder or the argument is upper or lower case, or a mix of upper and lower.

So if the foldername is "My Folder" and I run is_dir("mY FoLdEr") the result should be true.

Best Solution

That's not up to PHP. That's up to operating system and filesystem that PHP is running on. Windows is case insensitive but every version of Unix/Linux is case sensitive.

Let me put it another way: is_dir() is basically a wrapper to a system call or it will use the results of system calls on file info. Those system calls will either return something or not if, by the rules of that operating system and filesystem, a file is found with the matching name. PHP can't change this so no you can't just make is_dir() be case insensitive on Linux.

The best you can do is get a list of files in the directory and loop through them to test to see if any match a case insensitive comparison to what you're looking for.

Note: you might get multiple hits eg "HOME" and "Home" will both match "home". What would such a function (that you want) do in this case?

Alternatively you can change all your filenames to lowercase and then you can use the lowercase version of your input to find the specified file.