Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #11332

Re: reading files with accents in the filename from PHP

Message-ID <1818706.Kshgq7Ysi1@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-10-10 03:07 +0200
Subject Re: reading files with accents in the filename from PHP
Newsgroups comp.lang.php
References <525530ed$0$15878$e4fe514c@news2.news.xs4all.nl> <l33csp$qls$1@news.albasani.net> <52553b1f$0$15891$e4fe514c@news2.news.xs4all.nl> <l33e2m$sl2$1@news.albasani.net> <525570ee$0$15918$e4fe514c@news2.news.xs4all.nl>

Show all headers | View raw


Erwin Moller wrote:

> On 10/9/2013 1:18 PM, The Natural Philosopher wrote:
>> On 09/10/13 12:16, Erwin Moller wrote:
>>> On 10/9/2013 12:58 PM, Thomas Mlynarczyk wrote:
>>>> Erwin Moller schrieb:
>>>>> How can PHP open files on the local filesystem that contain certain
>>>>> characters, like umlauts, accents, etc?
>>>>
>>>> $path = __DIR__ . '\Eugène.txt';
>>>> var_dump( PHP_VERSION, file_exists( $path ) );
>>>
>>> That didn't help since my files are not stored in working dir.
>>>
>>>> Works on my Windows XP, PHP 5.4.8, *if* the PHP file is stored in ANSI
>>>> (="Windows") encoding. Doesn't work if stored in UTF8.
>>>
>>> Strange situation.
>>> I changed my PHP-files encoding to UTF-8, but the problem still
>>> occurred.
>>> […]
>>> I added a replace:
>>> $path = str_replace("è","\xE8",$path);
>>>
>>> and now it IS readable from PHP.
> […]
> I don't have a good feeling about my "fix".

And you should not.

> It worked, but I don't know exactly what is going on.

Exactly.
 
> I actually hoped PHP would handle such things 'the right way', whatever
> that might be. ;-)

PHP has no built-in support for character encodings (but it has extensions 
for that).  Your strings are read octet-wise from lowest to highest address 
as they are, that is, as the *editor* encoded the characters between the 
string delimiters.  If you write “"è"” in an UTF-8 encoded source file, the 
character between the delimiters will be encoded C3 A8.  If you write the 
*same* character in a Windows-1252-encoded source file, it will be encoded 
E8.

If your filesystem is FAT32, it will probably expect 8A if its locale is 
English (IBM437) or Central European (IBM850), for example.  If your 
filesystem is NTFS, it will expect E8 00 (UTF-16_LE_; my mistake); if you 
omit the zero octet it *might* work, but it does not work reliably.

> Now I wonder what happens if my code happens to run on some *nix OS.

The operating system is not the issue; the filesystem is.  However, usually 
Linux will run on ext2 to ext4, where AFAIK any character encoding can be 
used.  So there is a good chance that your code will break there.

> Ideally my PHP code is OS agnostic.

In that case you will probably have to detect the filesystem, and its 
encoding, and use the encoding that is expected by the filesystem.  Or 
prevent such filenames from occurring in the first place.

I suggest to encode PHP source files with UTF-8 _without BOM_.  If you write 
non-ASCII characters, you know what the encoding is, and you have a greater 
character set so that fewer characters need to be escaped.


PointedEars
-- 
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

reading files with accents in the filename from PHP Erwin Moller <erwinmollerusenet@xs4all.nl> - 2013-10-09 12:33 +0200
  Re: reading files with accents in the filename from PHP Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-10-09 12:58 +0200
    Re: reading files with accents in the filename from PHP Erwin Moller <erwinmollerusenet@xs4all.nl> - 2013-10-09 13:16 +0200
      Re: reading files with accents in the filename from PHP The Natural Philosopher <tnp@invalid.invalid> - 2013-10-09 12:18 +0100
        Re: reading files with accents in the filename from PHP Erwin Moller <erwinmollerusenet@xs4all.nl> - 2013-10-09 17:06 +0200
          Re: reading files with accents in the filename from PHP "Peter H. Coffin" <hellsop@ninehells.com> - 2013-10-09 10:51 -0500
          Re: reading files with accents in the filename from PHP "J.O. Aho" <user@example.net> - 2013-10-09 20:06 +0200
          Re: reading files with accents in the filename from PHP Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-10 03:07 +0200
    Re: reading files with accents in the filename from PHP Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-09 22:54 +0200
      Re: reading files with accents in the filename from PHP Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-09 23:33 +0200
        Re: reading files with accents in the filename from PHP Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-10 00:41 +0200
          Re: reading files with accents in the filename from PHP Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-31 21:34 +0100

csiph-web