4

file_exists("file_å.txt");

Returns false while file is there. Is there any what to make *file_exists* to see the files with this kind of names?

Thanks.

Aleksandr Makov
  • 2,820
  • 3
  • 37
  • 62

4 Answers4

3

I've run in to this a couple of times when for example reading filenames from an Excel and looking up a given path on disk. The problem may be that "ä" is either it's specific unicode char (\u00e4) or its "combined diaresis" form (a\u0308) ie an "a" with two dots.

Best solution since php 5.3 is to use the Normalizer library. You may need sudo apt-get install php5-intl to get the class into your PHP. After that just

$normalizer = new \Normalizer();
$normalizer->normalize($val, \Normalizer::FORM_D)

Theres a few different forms for the second argument so you'll have to look up which one you need

2

try

file_exists(mb_convert_encoding("file_å.txt", "UTF-8"));

assuming the text is not utf-8

Venu
  • 7,243
  • 4
  • 39
  • 54
2

Check this answer: https://stackoverflow.com/a/2685818/190791

The solution for me was to convert to windows file format:

$winfilename= iconv('utf-8', 'cp1252', $utffilename);
Community
  • 1
  • 1
Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136
1

Check which encoding your php script is in. Probably it's different from the file systems. (E.g. latin1 vs. utf-8)

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • Yeah, the file is UTF-8. Not sure how to check what encoding is my hoister's filesystem. Will keep you updated. And thanks for the tip. – Aleksandr Makov May 24 '12 at 14:10
  • 1
    Are you sure that your php file is in UTF8? (not the file_å.txt file) – periklis May 24 '12 at 14:16
  • iconv('UTF-8', 'ISO-8859-1', 'file_å.txt') provides proper file naming in the browser, but still, *file_eixsts* cannot check if it exists. – Aleksandr Makov May 24 '12 at 14:29
  • Strange - Maybe try to run `scandir` on the folder which contains your file and see how it comes back to you? – troelskn May 25 '12 at 05:45