PHP file_put_contents 和 UTF-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11115533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP file_put_contents and UTF-8
提问by Bounce
I have script that reads remote file content and writes it to local server. File contains characters: ???????ū?. After data insertion into local file, UTF-8 encoding is lost. My script code:
我有读取远程文件内容并将其写入本地服务器的脚本。文件包含字符:???????ū?。数据插入本地文件后,UTF-8 编码丢失。我的脚本代码:
<?php
$data = file_get_contents('remote_file_address');
echo $data; //encoding is ok
$file = dirname(__FILE__) . '/../downloads/data.csv';
file_put_contents($file,$data); //invalid encoding in data.csv file
?>
I also followed the instructions depending this post(How to write file in UTF-8 format?), but still no good.
我也按照这篇文章的说明(如何以 UTF-8 格式编写文件?),但仍然不好。
So what is wrong with that? Any ideas?
那么这有什么问题呢?有任何想法吗?
回答by Bounce
The problem was remote file with windows-1257 encoding. I found the solution here.
问题是使用 windows-1257 编码的远程文件。我在这里找到了解决方案。
So the correct code should look like this:
所以正确的代码应该是这样的:
<?php
$data = file_get_contents('remote_file_address');
$data = iconv("CP1257","UTF-8", $data);
$file = dirname(__FILE__) . '/../downloads/data.csv';
file_put_contents($file,$data);
?>
回答by deceze
PHP does not know about encodings. Strings in PHP are simply byte arrays that store raw bytes. When reading from somewhere into a string, the text is read in raw bytes and stored in raw bytes. When writing to a file, PHP writes the raw bytes into the file. PHP does not convert encodings by itself at any point.You do not need to do anything special at any point, all you need to do is to notmess with the encoding yourself. If the encoding was UTF-8 to begin with, it'll still be UTF-8 if you didn't touch it.
PHP 不知道编码。PHP 中的字符串只是存储原始字节的字节数组。从某处读取字符串时,文本以原始字节读取并存储在原始字节中。写入文件时,PHP 会将原始字节写入文件。PHP 在任何时候都不会自行转换编码。您不需要在任何时候做任何特别的事情,您需要做的就是不要自己弄乱编码。如果开始时编码是 UTF-8,如果你不碰它,它仍然是 UTF-8。
If the encoding is weird when opening the final file in some other program, most likely that other program is misinterpreting the encoding. The file is fine, it's simply not being displayed correctly.
如果在其他程序中打开最终文件时编码很奇怪,很可能是其他程序误解了编码。该文件很好,只是没有正确显示。
回答by niconoe
Be sure your script and the remote file is encoded in UTF-8 and be sure the soft you're using to read your data.csv read it in UTF-8. I personnaly use Notepad++ to check this. If all of your stuff is in UTF-8, you don't need any *utf8_(en|de)code function. You'll must use them if your remote file is not encoded in UTF-8
确保您的脚本和远程文件以 UTF-8 编码,并确保您用来读取 data.csv 的软件以 UTF-8 读取它。我个人使用 Notepad++ 来检查这个。如果你所有的东西都是 UTF-8,你就不需要任何 *utf8_(en|de)code 函数。如果您的远程文件不是以 UTF-8 编码,则必须使用它们

