mime的php扩展,如何在PHP中确定与MIME类型相关联的扩展?

不是内置的,但滚动自己不是很难:

function system_extension_mime_types() {

# Returns the system MIME type mapping of extensions to MIME types, as defined in /etc/mime.types.

$out = array();

$file = fopen('/etc/mime.types', 'r');

while(($line = fgets($file)) !== false) {

$line = trim(preg_replace('/#.*/', '', $line));

if(!$line)

continue;

$parts = preg_split('/\s+/', $line);

if(count($parts) == 1)

continue;

$type = array_shift($parts);

foreach($parts as $part)

$out[$part] = $type;

}

fclose($file);

return $out;

}

function system_extension_mime_type($file) {

# Returns the system MIME type (as defined in /etc/mime.types) for the filename specified.

#

# $file - the filename to examine

static $types;

if(!isset($types))

$types = system_extension_mime_types();

$ext = pathinfo($file, PATHINFO_EXTENSION);

if(!$ext)

$ext = $file;

$ext = strtolower($ext);

return isset($types[$ext]) ? $types[$ext] : null;

}

function system_mime_type_extensions() {

# Returns the system MIME type mapping of MIME types to extensions, as defined in /etc/mime.types (considering the first

# extension listed to be canonical).

$out = array();

$file = fopen('/etc/mime.types', 'r');

while(($line = fgets($file)) !== false) {

$line = trim(preg_replace('/#.*/', '', $line));

if(!$line)

continue;

$parts = preg_split('/\s+/', $line);

if(count($parts) == 1)

continue;

$type = array_shift($parts);

if(!isset($out[$type]))

$out[$type] = array_shift($parts);

}

fclose($file);

return $out;

}

function system_mime_type_extension($type) {

# Returns the canonical file extension for the MIME type specified, as defined in /etc/mime.types (considering the first

# extension listed to be canonical).

#

# $type - the MIME type

static $exts;

if(!isset($exts))

$exts = system_mime_type_extensions();

return isset($exts[$type]) ? $exts[$type] : null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值