UTF-8, PHP, Win7 - Is there a solution now to save

2019-04-29 19:33发布

Update: Just to not make you reading through all: PHP starting with 7.1.0alpha2 supports UTF-8 filenames on Windows. (Thanks to Anatol-Belski!)

Following some link chains on stackoverflow I found part of the answer:
https://stackoverflow.com/a/10138133/3716796 by Umberto Salsi
(and on the same question: https://stackoverflow.com/a/2950046/3716796 by Artefacto)
In short: 'PHP communicate[s] with the underlying file system as a "non-Unicode aware program"', and because of that all filenames given to PHP by Windows and vice versa are automatically translated/reencoded by Windows. This causes the errors. And you seemingly can't stop the automatic reencoding.
(And https://stackoverflow.com/a/2888039/3716796 by Artefacto:
"PHP does not use the wide WIN32 API calls, so you're limited by the codepage.")
And at https://bugs.php.net/bug.php?id=47096 there is the bug report for PHP.

Though on there nicolas suggests, that a COM-object might work!
$fs = new COM('Scripting.FileSystemObject', null, CP_UTF8);

Maybe I will try that sometimes.

So there is the part of my questionleft : Is there PHP6 out, or was it withdrawn, or is there anything new on PHP about that topic?

// full Question
The most questions about this topic are 1 to 5 years old. Could php now save a file using file_put_contents($dir . '/' . $_POST['fileName'], $_POST['content']);

when the $_POST['fileName'] is UTF-8 encoded, for example "Крым.xml" ? Currently it is saved as Крым.xml

I checked the fileName variable, so I can be sure it's UTF-8: echo mb_detect_encoding($_POST['fileName']);

  1. Is there now anything new in PHP that could accomplish it?
  2. At some places I read PHP 6 would be able to do it, but PHP 6 if i I remember right, has been withdrawn. ?
  3. In Windows Explorer I can change the name of a file to "Крым.xml". As far as I have understood the old questions&answers, it should be possible to use file_put_contents if the fileName-var is simply encoded to the encoding used by windows 7 and it's NTFS disc. There is even 3 old question with answers that claim to have succeeded:
    PHP File Handling with UTF-8 Special Characters
    Convert UTF-16LE to UTF-8 in php
    and PHP: How to create unicode filenames

    Overall and most approved answers say it is not possible. I checked all suggested answers already myself, and none works.
  4. How to definitly and with absolute accuracy find out, in which encoding my Win 7 and Explorer saves the filename on my NTFS disc and with German language setting? As said: I can create a file "Крым.xml" in the Explorer.

My conclusion:
1. Either file_put_contents doesn'T work correctly when handing over the fileName (which I tried with conversions to UTF-16, UTF-16LE, ISO-8859-1 and Windows-1252) to Windows,
2. or file_put_contents just doesn't implement a way to call Windows' own file function in the appropriate way (so this second possibility would mean it's not a bug but just not implemented.) (For example notepad++ has no problems creating, writing and renaming a file called Крым.xml.)

Just one example of the error messages I got, in this case when I used mb_convert_encoding($theFilename , 'Windows-1252' , 'UTF-8')

"Warning: file_put_contents(dirToSaveIn/????.xml): failed to open stream: No error in C:\aa xampp\htdocs\myinterface.lo\myinterface\phpWriteLocalSearchResponseXML.php on line 26 " With other conversion I got other error messages, ranging from 'invalid characters' to no string recognized at all.

Greetings John

1条回答
The star\"
2楼-- · 2019-04-29 20:05

PHP starting with 7.1.0alpha2 supports UTF-8 filenames on Windows.

Thanks.

查看更多
登录 后发表回答