Laravel 5 - 从文件扩展名获取 MIME 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29509778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 - get MIME type from file extension
提问by Limon Monte
In Laravel 5 how can I get MIME type from extension? Even better if there's a way to convert array of extensions to array of mimes.
在 Laravel 5 中,如何从扩展中获取 MIME 类型?如果有一种方法可以将扩展数组转换为 mime 数组,那就更好了。
E.g. how to convert array('doc', 'xls')
to array('application/msword', 'application/vnd.ms-excel')
?
例如如何转换array('doc', 'xls')
为array('application/msword', 'application/vnd.ms-excel')
?
回答by Juukie14
When "guzzlehttp/guzzle": "~5.3|~6.0" is in your composer.json, you can use this:
当 "guzzlehttp/guzzle": "~5.3|~6.0" 在你的 composer.json 中时,你可以使用这个:
$mimetype = \GuzzleHttp\Psr7\mimetype_from_filename('foo.doc');
$mimetype = \GuzzleHttp\Psr7\mimetype_from_extension('doc');
回答by Umair Hamid
Simply the best in L5:
简直是 L5 中最好的:
\File::mimeType('physical/path/to/file.ext');
回答by Limon Monte
Guzzleis included in Laravel 5 by default, there's the list of mime typesin this library and fromExtension()
method which do exaclty what was asked.
默认情况下,Guzzle包含在 Laravel 5中,这个库和方法中的 mime 类型列表可以准确地fromExtension()
完成所要求的内容。
So, to get MIME type of single extension:
因此,要获得 MIME 类型的单个扩展名:
$mimetypes = new \GuzzleHttp\Mimetypes;
$mime = $mimetypes->fromExtension($extension);
To get array of MIME types from array of extensions:
从扩展数组中获取 MIME 类型数组:
$mimetypes = new \GuzzleHttp\Mimetypes;
$mimes = [];
foreach ($extensions as $extension) {
$mimes[] = $mimetypes->fromExtension($extension);
}
回答by Rana Nadeem
to get file mimetype
获取文件 mimetype
$request->file->getMimeType()
validation code
验证码
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg'
'mp3'=>'required|mimetypes:audio/mpeg'
]);
you can get the file type from above code, after that you can set that to mimetypeslike set for mp3
您可以从上面的代码中获取文件类型,之后您可以将其设置为像 set for mp3 这样的mimetypes
回答by ByteHamster
First, you need to download this public domain file: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
首先,您需要下载这个公共域文件:http: //svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
Then use the following function to read the file and get the corresponding MIME
type for an extension:
然后使用以下函数读取文件并获取MIME
扩展名的相应类型:
function getMIME($extension) {
$file = "mime.types";
$in = fopen($file, "r");
while (($line = fgets($in)) !== false) {
if (preg_match("/([a-z]+\/[a-z]+)\s+([a-z\s]*)\b($extension)\b/", $line, $match)) {
return $match[1];
}
}
fclose($in);
return "error";
}
echo getMIME("doc");
Output:
输出:
application/msword
应用程序/msword
To convert the array:
要转换数组:
$myArray = array('doc', 'xls');
foreach($myArray as $key => $value){
$myArray[$key] = getMIME($value);
}
回答by Eman Dev
MimeType::from('koala_transparent.png')
return "image/png"
返回“图像/png”
回答by Solo.dmitry
I used
我用了
https://github.com/ralouphie/mimey
https://github.com/ralouphie/mimey
it can give mimetype by extension without existing file
它可以在没有现有文件的情况下通过扩展名给出 mimetype