Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型) [英] Replacement for File::mime() in Laravel 4 (to get mime type from file extension)

查看:22
本文介绍了Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 3 有一个 File::mime() 可以很容易地从扩展名中获取文件的 mime 类型的方法:

Laravel 3 had a File::mime() method which made it easy to get a file's mime type from its extension:

$extension = File::extension($path);
$mime = File::mime($extension);

升级到 Laravel 4 时出现错误:

On upgrading to Laravel 4 I get an error:

调用未定义的方法 IlluminateFilesystemFilesystem::mime()

我在 Filesystem API 文档中也看不到任何提及 mime 类型的内容.

在 Laravel 4 中获取文件 mime 类型的推荐方法是什么(请注意这不是用户上传的文件)?

What's the recommended way to get a file's mime type in Laravel 4 (please note this is not a user-uploaded file)?

推荐答案

我发现的一个解决方案是使用 Symfony HttpFoundation File 类(它已经作为依赖包含在 Laravel 4 中):

One solution I've found is to use the Symfony HttpFoundation File class (which is already included as a dependency in Laravel 4):

$file = new SymfonyComponentHttpFoundationFileFile($path);
$mime = $file->getMimeType();

实际上 File 类使用 Symfony MimeTypeGuesser 类所以这也有效:

And in fact the File class uses the Symfony MimeTypeGuesser class so this also works:

$guesser = SymfonyComponentHttpFoundationFileMimeTypeMimeTypeGuesser::getInstance();
echo $guesser->guess($path);

但不幸的是,我得到了意想不到的结果:将路径传递给 css 文件时,我得到的是 text/plain 而不是 text/css.

But unfortunately I'm getting unexpected results: I'm getting text/plain instead of text/css when passing a path to a css file.

这篇关于Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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