获取给定URL的MIME/Media/Content类型的最简单,最快的方法是什么? [英] What's the easiest and quickest way to get the MIME/Media/Content type of a given URL?

查看:139
本文介绍了获取给定URL的MIME/Media/Content类型的最简单,最快的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我打算将文件从远程服务器传输到服务器,但是在传输文件之前,我想知道它是哪种文件.如果它是HTML文件,我将以某种方式处理它,如果它是PDF文件,则将以另一种方式处理它,例如,如果它是一个exe文件,我不想传输完全没有.

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, and if, for instance, it's an exe file, I don't want to transfer it at all.

我要做的只是一个URL.现在,如果该网址是明确的(即 http://example.com/file.pdf ),则该网址不是问题,我只需要检查文件扩展名就可以了.但是,可能只会给我一个URL,例如 http://example.com ,它可以是任何东西.

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.

在下载文件之前,是否可以使用PHP检查文件的类型?

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

:我将使用cURL传输文件,但我相信

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?

推荐答案

尝试使用PHP

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

这篇关于获取给定URL的MIME/Media/Content类型的最简单,最快的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆