0

A drop-box directory for image files has collected variants by letter-case, for example:

Bonsai.jpg, BONSAI.jpg, Bonsai.JPG, bonsai.jpg  

I am making a web app using CodeIgniter to manage these documents on a remote server. This means using

  1. file_exists() or is_file() to verify a file's presence
  2. HTML img tag to display the file graphically

But both these tools use the first match they find, regardless of case. How can I deal with this?

(I noticed this similar question as this, but for Delphi instead of PHP.)

Community
  • 1
  • 1
Smandoli
  • 6,919
  • 3
  • 49
  • 83
  • The Back Story: Due to inadequate planning and my lack of understanding of how to manage files on a web server, the doc repository is a mess. I now must (A) establish a file naming standard (e.g. all lower-case), (B) identify and resolve duplicates, (C) convert file names to the new standard, and (D) develop the app to enforce the guidelines. And there are MANY of these. I'm well used to data normlzation, but this is a complicated version. I plan to download everything for (B) and (C), and I will be able to work in stages. – Smandoli Aug 24 '10 at 17:44
  • 1
    You can easily write a script that puts all filenames in to an array, identify duplicates and append _1 to their name. Now you have just unique filenames. Then you convert all to lowercase. For all existing files and new ones you encrypt the filenames to a 32 character string. Batch processing of filenames like this is actually quite easy. Just keep a back up of all files just in case, and very little can go wrong. – stef Aug 25 '10 at 09:46
  • @stef -- that looks great -- many thanks for laying it out so simple. I would have dumped it all local and used python, but if I can do it with PHP on-site, that is better. – Smandoli Aug 25 '10 at 12:28

2 Answers2

1

But both these tools use the first match they find, regardless of case

They definitely shouldn't - at least not on a file system that is case sensitive, like Linux's default file system (is it still called ext2?). While it's questionable practice to have those four file in the same directory IMO, neither file_exists() nor the serving of web resources should show the behaviour you describe.

It's different on Windows: FAT and NTFS are not case sensitive. In your example, only one of the four files you mention can exist in the same directory.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • '... it's questionable practice to have those four files ...' Yes, certainly! The idea is to clean it up, to start with. – Smandoli Aug 24 '10 at 03:09
  • Okay, I have more clarity now. My LAMP setup works as case-sensitive; my WAMP set up does not -- just as you say. – Smandoli Aug 24 '10 at 03:10
  • This means my working goals aren't compromised, since WAMP is the odd member of the setup. Maybe I will set up my test suite to adjust for environment, so I can enjoy the green bar everywhere. – Smandoli Aug 24 '10 at 03:13
  • @Smandoli ah, I see! This indeed makes a tricky test case. That may indeed call for the test to adjust for environment. – Pekka Aug 24 '10 at 03:17
  • @Smandoli maybe test for `PHP_WINDOWS_VERSION_MAJOR`? http://php.net/manual/en/reserved.constants.php – Pekka Aug 24 '10 at 03:21
  • Interesting! But I only need something to distinguish between my two devs and the production server. Something simple like SERVER_NAME, but haven't yet found one that is reliably on all three yet ... – Smandoli Aug 24 '10 at 05:09
1

When accepting images I always rename them, for example using CI's encrypt filenames option of the File Upload class to avoid these kind of problems. Otherwise it can turn in to a big headache.

EDIT: added my comment on the OP below

You can easily write a script that puts all filenames in to an array, identify duplicates and append _1 to their name. Now you have just unique filenames. Then you convert all to lowercase. For all existing files and new ones you encrypt the filenames to a 32 character string. Batch processing of filenames like this is actually quite easy. Just keep a back up of all files just in case, and very little can go wrong.

Codeigniter has some useful functions like the file helper's get_filenames() which puts all files in a specified directory in to an array, and the security helper's dohash() which would encrypt the filenames. For future uploads set encrypt_name preference to TRUE

stef
  • 26,771
  • 31
  • 105
  • 143
  • See 'The Back Story' comment on the OP. Your tip here may be extremely useful. – Smandoli Aug 24 '10 at 17:45
  • +1 This is a hugely helpful set of information. Pekka gets my "Accept" because his response was direct to my question. (Now if only one always knew what questions to ask ... ) :-) – Smandoli Aug 26 '10 at 15:09