为什么 Windows 需要使用 utf8_decode 文件名才能让 `file_get_contents` 工作? [英] Why does Windows need to `utf8_decode` filenames for `file_get_contents` to work?

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

问题描述

如果 $filename 包含变音符号 (ä,ö,ü) 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;

因为这已经解决了同样的问题(与 utf8_encode 有同样的问题),但是 $filename 在每个(服务器)环境,所以这不起作用,因为它总是正确的.

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 进行 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 需要使用 utf8_decode 文件名才能让 `file_get_contents` 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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