php获取远程文件后缀,PHP获取远程文件后缀

try {

$url = 'http://nos.netease.com/edu-lesson-pdfsrc/1876F35C47CA72ABB8CEBBFCEC977149-1545094506108?Signature=16yL1%2Fh0syOXtPS9ydfWUFXF4PoZwE5vJTedcZivcDA%3D&Expires=1556782895&NOSAccessKeyId=7ba71f968e4340f1ab476ecb300190fa&download=%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%B5%B7%E6%BA%90.pdf';

$url = 'http://jdvodrvfb210d.vod.126.net/jdvodrvfb210d/nos/mp4/2018/12/16/1010886261_c51c5d93ba0b4960b2ebc7a3be7a9cf9_hd.mp4';

echo get_remote_file_ext($url);

} catch(Exception $e) {

echo $e->getMessage();

}

function get_remote_file_ext($url) {

$ext = 'unknown';

stream_context_set_default(array('http'=>array('method'=>'GET', 'Host' => 'nos.netease.com')));

$headers = get_headers($url, 1);

if(isset($headers[0])) {

$splits = explode(' ', $headers[0]);

if(count($splits) < 2 ) {

throw new Exception($headers[0]);

return $ext;

}

if($splits[1] != '200') {

throw new Exception($splits[1]);

return $ext;

}

}

if(isset($headers['Content-Disposition'])) {

if(preg_match('/filename="[\S]*?\.([\w]*?)"/i', $headers['Content-Disposition'], $match)) {

$ext = $match[1];

}

} elseif (isset($headers['Content-Type'])) {

if(preg_match('/([\S]*?);/i', $headers['Content-Type'], $match)) {

$mime_type = $match[1];

if(($mime = get_mime($mime_type)) == -1) {

return $ext;

} else {

$ext = $mime;

}

}

}

return $ext;

}

function get_mime($mime_type) {

$match_mime = -1;

// mime列表:https://github.com/bcit-ci/CodeIgniter/blob/master/application/config/mimes.php

$mimes = array(

'hqx'=>array('application/mac-binhex40', 'application/mac-binhex', 'application/x-binhex40', 'application/x-mac-binhex40'),

'cpt'=>'application/mac-compactpro',

'mp4' =>'video/mp4',

);

foreach($mimes as $mime => $mime_types) {

if(gettype($mime_types) == 'array') {

if(in_array($mime_type, $mime_types)) {

$match_mime = $mime;

break;

}

} elseif(gettype($mime_types) == 'string') {

if($mime_type == $mime_types) {

$match_mime = $mime;

break;

}

}

}

return $match_mime;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值