开发者

What's the easiest and quickest way to get the MIME/Media/Content type of a given URL?

开发者 https://www.devze.com 2023-04-10 06:47 出处:网络
So I\'m going to transfer a file from a remote server to my server but before I transfer it I want to know what sort of file it is.If it\'s an HTML file I\'ll deal with it in a certain way, if it\'s a

So I'm going to transfer a file from a remote server to my server but before I transfer it I want to know what sort of file it is. If it's an HTML file I'll deal with it in a certain way, if it's a PDF file I'll deal with it in a different way, an开发者_如何学God if, for instance, it's an exe file, I don't want to transfer it at all.

All I've got to go on is a URL. Now if that URL is explicit (i.e. http://example.com/file.pdf) it's not a problem, I can just check the file extension and we're sorted. However, I might only be given a URL such as http://example.com and it could be anything.

Is it possible to check (with PHP) what type of file it is before I download it?

Edit: I will be using cURL to transfer the file, but I believe that curl_getinfo can only be called after the file has been transferred?


Try using PHP get_headers function, e.g.:

$h = get_headers("http://google.com", $format = 1);
$content_type = $h["Content-Type"];
// and now parse what you want from array


Using an fopen handle, you can then call stream_get_meta_data() on it.

0

精彩评论

暂无评论...
验证码 换一张
取 消