PHP-重音文件名上的读取文件
我正在尝试显示名称包含口音的图像。
文件名的示例:
雀巢 - 咖啡叶 - 液体-lite-311g.jpg
最初是我通过直接从公共文件夹访问图像来显示我的图像
<img src="[sitename]/images/products/Nestlé-Coffee-Mate-Original-Lite-311g.jpg"/>
来获取图像(PHP)
,但是只有没有口音的图像显示,我必须切换以通过我的后端(PHP)切换 我有类似的内容:
<img src="[sitename]/api/image/products/Nestlé-Coffee-Mate-Original-Lite-311g.jpg"/>
PHP代码:
public function image($folder, $name) {
header("Content-type: image");
return readfile(FCPATH . 'images/' . $folder . '/' . $name);
}
- 有什么方法可以直接从公共文件夹中访问带有重音名称的文件而无需通过PHP获取,因为这会减慢图像的显示。
- 而且,如果我不得不因为口音而必须通过PHP获取,那么正确的方法是什么?
我尝试在文件名上使用encodeuri
,该文件导致类似的内容:
<img src="[sitename]/api/image/products/Nestle%CC%81-Coffee-Mate-Original-Lite-311g.jpg" />
在php中:
return readfile(FCPATH . 'images/' . $folder . '/' .rawurldecode($name));
//or
return readfile(FCPATH . 'images/' . $folder . '/' .urldecode($name));
//or
return readfile(FCPATH . 'images/' . $folder . '/' .utf8_decode($name));
但是上面都没有。
我还尝试了Stackoverflow的各种解决方案,但没有任何解决方案。
I am trying to display images whose names contain accents.
example of a filename:
Nestlé-Coffee-Mate-Original-Lite-311g.jpg
Originally I was displaying my images by accessing them directly from the public folder
<img src="[sitename]/images/products/Nestlé-Coffee-Mate-Original-Lite-311g.jpg"/>
But only images without accents was displaying and I had to switch to fetching the images via my backend (PHP)
Now I have got something like:
<img src="[sitename]/api/image/products/Nestlé-Coffee-Mate-Original-Lite-311g.jpg"/>
PHP code:
public function image($folder, $name) {
header("Content-type: image");
return readfile(FCPATH . 'images/' . $folder . '/' . $name);
}
- Is there any way I can access files with accented names directly from the public folder without having to fetch via PHP because this slows the displaying of images a bit.
- And if I imperatively have to fetch via PHP because of the accents, what is the proper way to do it?
I have tried using encodeURI
on the filename which resulted in something like:
<img src="[sitename]/api/image/products/Nestle%CC%81-Coffee-Mate-Original-Lite-311g.jpg" />
and in PHP:
return readfile(FCPATH . 'images/' . $folder . '/' .rawurldecode($name));
//or
return readfile(FCPATH . 'images/' . $folder . '/' .urldecode($name));
//or
return readfile(FCPATH . 'images/' . $folder . '/' .utf8_decode($name));
but none of the above worked.
I have also tried various solutions from StackOverflow but none worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UTF-8中有两种不同的强调字符表示,并分解了。
é
\ u00e9
\ code> \ xc3 \ xa9
e e 的组合标记
\ u0065
\ x65
以及Accent\ u0301
\ xcc \ xcc \ x81
也就是说,在使用UTF-8的文件名接受文件名时,您应该在将这些名称写入磁盘和/或创建链接之前,以避免此问题,以避免此问题和其他问题。
要归一化为组成和分解的表格:
输出:
您还应检查OS和文件系统如何处理UTF-8文件名。有些人只会将任何系列字节写为文件名,有些会很挑剔和拒绝,而另一些字节将执行自己的标准化,而这些标准化可能并不符合您在应用程序中选择的内容。
There are two different representations of common accented characters in UTF-8, composed, and decomposed.
é
\u00e9
\xC3\xA9
e
\u0065
\x65
and the combining mark for the accent\u0301
\xCC\x81
What you have posted in your question uses the latter, decomposed form. You may, in the short term, get your request to work by supplying the comosed form of that character.
That said, when accepting filenames with UTF-8 you should make a point of normalizing those names before writing them to disk, and/or creating links, to avoid this and other problems.
To normalize to Composed and Decomposed forms:
Output:
You should also check how your OS and filesystem handle UTF-8 filenames. Some will simply write any series of bytes as the filename, some will be picky and reject, and others will perform their own normalization that may not neccessarily match what you have chosen in your app.