有没有办法从 JS 或 PHP 中的 Google 驱动器 link 中找出 mime 类型?
Is there a way to find out the mime type from a Google Drive link in JS or PHP?
我想将一些 google 驱动器文件的 url 连同 MIME 类型一起保存到数据库中。在 JavaScript(最好)或 PHP 中有什么方法可以检查 url 并找出它是什么类型的文件? audio/mp3
、video/mp4
等等?
这是文件的语法,但不幸的是,无法根据 url 判断它是哪种文件:https://drive.google.com/uc?export=download&id=0B_Tg8P0D8mAYUHlrX3NWRmwwOGc
感谢线索!
在 PHP 中,您可以使用带有 CURLINFO_CONTENT_TYPE
标志的 cURLs curl_getinfo
函数
<?php
// the request
$ch = curl_init($documentURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
// get the content type
echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
?>
您的输出将与 text/html; charset=ISO-8859-1
一致,您可以 explode
只获取 MIME 类型:
<?php
$mime = explode(";", curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
echo $mime[0];
?>
我想将一些 google 驱动器文件的 url 连同 MIME 类型一起保存到数据库中。在 JavaScript(最好)或 PHP 中有什么方法可以检查 url 并找出它是什么类型的文件? audio/mp3
、video/mp4
等等?
这是文件的语法,但不幸的是,无法根据 url 判断它是哪种文件:https://drive.google.com/uc?export=download&id=0B_Tg8P0D8mAYUHlrX3NWRmwwOGc
感谢线索!
在 PHP 中,您可以使用带有 CURLINFO_CONTENT_TYPE
标志的 cURLs curl_getinfo
函数
<?php
// the request
$ch = curl_init($documentURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
// get the content type
echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
?>
您的输出将与 text/html; charset=ISO-8859-1
一致,您可以 explode
只获取 MIME 类型:
<?php
$mime = explode(";", curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
echo $mime[0];
?>