Click to See Complete Forum and Search --> : file_exists() case sensitivity
tarsus
03-28-2008, 01:06 PM
Is the case sensitivity of file_exists() determined merely by the operating system? (i.e., is the result case sensitive on Linux because that OS's file system is?)
If that's the case, is there a good way to do a case INsensitive check for the existence of a file on Linux? Or is this a case, like most concerning this issue, where a workaround would be complicated and sticky?
Znupi
03-28-2008, 01:11 PM
The only way I see it is by going through all the files in the directory where you want to check if the file exists and check each one with strtolower($dirfile) == strtolower($checkfile). Tell us if you need help with that.
tarsus
03-28-2008, 01:13 PM
Doubt that I will go to that length to accomplish it. But you are confirming that the simple function is case sensitive on Linux, right?
Znupi
03-28-2008, 01:30 PM
Yes. But it's not hard to do what I said. Here's a function you can use just like you use file_exists:
function file_iexists($path) {
$dirname = dirname($path);
$filename = basename($path);
$dir = dir($dirname);
while ( ($file = $dir->read()) !== false) {
if (strtolower($file) == strtolower($filename)) {
$dir->close();
return true;
}
}
$dir->close();
return false;
}
Try that :)
Edit: That won't work if you got the directory name in the wrong case. Explanation:
// Say we have the file: /path/to/FILE.txt
file_iexists('/path/to/file.txt'); // true
file_iexists('/PATH/TO/FILE.txt'); // false