Why does Windows need to `utf8_decode` filenames for `file_get_contents` to work?(为什么 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` 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 Windows 需要使用 utf8_decode 文件名才能让 `file_get_contents` 工作?
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
