为什么Windows需要`file_get_contents`的`utf8_decode`文件名来工作? [英] Why does Windows need to `utf8_decode` filenames for `file_get_contents` to work?

查看:114
本文介绍了为什么Windows需要`file_get_contents`的`utf8_decode`文件名来工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 $ filename 包含umlauts(ä,ö,ü) file_get_contents($ filename)在我的Windows操作系统。通过反复试验,我发现我需要做 file_get_contents(utf8_decode($ filename))才能正常工作。

If $filename contains umlauts (ä,ö,ü) file_get_contents($filename) doesn't work on my Windows OS. By trial and error I found out that I need to do file_get_contents(utf8_decode($filename)) to get it to work.

然而,当我把这个生活推送到我的服务器(猜测它是某种Linux),它再次返回一个错误,所以我删除了 utf8_decode ,突然它工作

However, when I pushed this live to my server (guess it's some kind of Linux) it returned an error again, so I removed the utf8_decode and suddenly it worked perfectly.

作为一种解决方法(所以我不需要每次更改代码时手动更改这段代码)我已经尝试过

As a workaround (so I don't need to change this piece of code manually every time I make changes to the code) I already tried

(mb_detect_encoding($filename, 'UTF-8', true)) ? utf8_decode$filename) : $filename;

因为这对于相同的问题已经工作了$ c> utf8_encode ),但是 $ filename 在每个(服务器)环境中都是UTF8编码的,因为它总是真的。

as this already worked for the same problem the other way round (had the same problem with utf8_encode), but $filename turned out to be UTF8-encoded in every (server) environment, so this doesn't work, as it's always true.

任何想法如何让这在两个系统上工作? (请不要刚刚迁移到Linux for PHP开发 - 我有Linux,但ATM我使用Windows的原因很多)

Any ideas how to get this to work on both systems? (Please no "just migrate to Linux for PHP development"—I've got Linux, but ATM I'm using Windows for a number of reasons)

编辑:问题也出现在 fopen ,并且接受的解决方案也可以。

the problem appears also with fopen and the accepted solution works as well.

推荐答案

最好的方法是检测是否使用Windows服务器。
从中可以应用正确的命令

The best way would be to detect if your using Windows server or not. From that you can apply the right command

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

这篇关于为什么Windows需要`file_get_contents`的`utf8_decode`文件名来工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆