[英]create a file (name.extension) by mime type
How to create a file (name_of_file . file_extension), by mime type? 如何通过mime类型创建文件(name_of_file.file_extension)? The list of mimes is dynamic.
哑剧列表是动态的。
Example: 例:
MIME Extension
application/msword | .doc
text/plain | .txt
text/css | .css
I dont wanna use array("mime" => "extension"); 我不想使用数组(“mime”=>“扩展名”);
I have the mime type, but i need the file extension. 我有mime类型,但我需要文件扩展名。
To avoid using a lookup table (at least avoid you having to define one) you can use finfo_file
. 为了避免使用查找表(至少避免你具有定义一个),可以使用
finfo_file
。
echo finfo_file('myfile.jpg', finfo_open(FILEINFO_MIME_TYPE)); // output: image/jpg
If you're on 5.0, you may try mime_content_type
(But keep in mind this is deprecated, so if you're looking for upgrade-compatible, you may want to default to the new finfo_file
when possible): 如果您使用5.0,则可以尝试使用
mime_content_type
(但请记住,这是不推荐使用的,因此如果您正在寻找兼容升级的版本,则可能需要在可能的情况下默认使用新的finfo_file
):
echo mime_content_type('myfile.jpg'); // output: image/jpg
// When possible, it will use upgraded finfo_file, but will default back
// to content_mime_type when necessary
function getMimeType($file){
if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')){
$return_mime = finfo_open(FILEINFO_MIME_TYPE);
return finfo_file($file, $return_mime);
}
return content_mime_type($file);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.