Know your ISP.

User #90775   1730 posts
Whirlpool Enthusiast

I had my data on Fat32 drive and when I used the function file_exists() I found it to be case insensitive. But now the data has been moved to a Ext3 partition (Linux) this function seems to be working with case sensitive.

Is there anyway to make it case insensitive? Rather than testing every file in the directory (which has been some of the suggestions I have found on the net)

posted 2008-May-11, 8am AEST
User #182751   140 posts
Forum Regular

I would expect the behaviour that you have mentioned.

File systems used by Windows are not case sensitive, I think the best they can do is preserve case.

If there was a case insensitive file exists function, it would need to generate and check every permutation.
eg:

FILE.JPG
fIlE.Jpg
File.Jpg
file.jpg
and so on. It wouldn't be very quick or efficient - and would take longer for longer file names.

Testing every file in the directory would be how I would do it. Maybe someone has a better solution?

posted 2008-May-11, 9am AEST
User #6707   61 posts
Forum Regular

Have to test every file in the directory, and lowercase both values (your search string, and the string returned from a directory parse) to guarantee its existence..

thats the only way i can see doing it..

posted 2008-May-11, 11am AEST
User #44690   10502 posts
Whirlpool Forums Addict

Testing for file existence is almost always a code smell -- it's almost always better to attempt to do something with the file, then handle the error of its non-existence afterwards.

Nevertheless, you can use the fnmatch to do what you want. Note that this function applies shell-style globs, so you will definitely want to backslash-escape the characters \, *, ?, [ and ] in the filename you're looking for.

To get fnmatch to match case-insensitively, you need to use a GNU extension, but luckily you're running this on a GNU system. The trick is to pass the FNM_CASEFOLD flag into the $flags parameter. This has the constant value 16 on GNU systems, but it isn't defined by PHP itself, so you might want to do so yourself using define.

posted 2008-May-11, 12pm AEST
edited 2008-May-11, 12pm AEST
User #28944   2031 posts
Whirlpool Forums Addict

The problem you will have though is what happens if there are two files?

'File.example' is not the same as 'file.Example' under linux, but if you do a case insensitive search, then you will be saying they are the same. What happens if you are then trying to process the file. You really want to process 'File.example', but you get 'file.Example'.

You should be strict when it comes to naming conventions. Most common practice is to keep everything lowercase.

posted 2008-May-11, 3pm AEST
User #90775   1730 posts
Whirlpool Enthusiast

Thanks, I will just go and manually rename the files.

posted 2008-May-12, 12pm AEST
edited 2008-May-12, 12pm AEST
Hosted by
WebCentral Australia
Big numbers
950,182 threads
16,717,378 posts
1,961,824 whims sent
2,994 wiki topics
238 ISPs listed
8,022 broadband plans
797 modems & routers
39,260 features filled