为什么 Symfony2 File->getMimeType() 为 CSS 文件返回 text/plain 而不是 text/css? [英] Why is Symfony2 File->getMimeType() returning text/plain not text/css for a CSS file?

查看:24
本文介绍了为什么 Symfony2 File->getMimeType() 为 CSS 文件返回 text/plain 而不是 text/css?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取服务器上文件的 MIME 类型.我正在使用 Laravel,但想使用来自 Symfony HTTPFoundation 的 File 类(请参阅我的相关问题).

I need to get the MIME type of a file on the server. I'm using Laravel but want to use the File class from Symfony HTTPFoundation (see my related question).

当我使用 文件->GetMimeType() 在 css 文件中,当我期待 text/css 时返回 text/plain.

When I use File->GetMimeType() on a css file, text/plain is returned when I'm expecting text/css.

$file = new Symfony\Component\HttpFoundation\File\File($path); //pass CSS file
$mime = $file->getMimeType(); //returns text/plain

我做错了什么?

推荐答案

查看我对这个问题的回答和简单的解决方案 - https://stackoverflow.com/a/26299430/1331510

See my answer to this question and simple solution - https://stackoverflow.com/a/26299430/1331510

然而,我的方法并不完美,但它适用于文件上传:只需从文件名中提取文件扩展名并使用 in:pdf,csv,css,.. 规则.

However, my approach is not perfect, but it works for file uploads: just extract file extension from file name and check its presence in and array with in:pdf,csv,css,.. rule.

这篇关于为什么 Symfony2 File->getMimeType() 为 CSS 文件返回 text/plain 而不是 text/css?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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