php怎么通过一个Url获得文件类型(后缀名)?

WBOY
Release: 2016-06-06 20:39:03
Original
1651 people have browsed it

网上找的方法都是知道文件后缀名的情况下~
我现在的情况是只知道Url,比如http://site.com/photo,这是一个文件,但是没有把文件类型和后缀在上面表示出来,我接收到之后怎么用Php把他后缀或者文件类型获取到?

好吧,我是做微信的时候用户发过来的图片腾讯只给了我一个这样的url

回复内容:

网上找的方法都是知道文件后缀名的情况下~
我现在的情况是只知道Url,比如http://site.com/photo,这是一个文件,但是没有把文件类型和后缀在上面表示出来,我接收到之后怎么用Php把他后缀或者文件类型获取到?

好吧,我是做微信的时候用户发过来的图片腾讯只给了我一个这样的url

http://php.net/manual/en/function.finfo-buffer.php

php怎么通过一个Url获得文件类型(后缀名)?

大家写的代码很不高效呀

<code><?php echo `curl -Is 'http://s11.sinaimg.cn/mw690/e0571d75tx6Co4vJcUOfa&690' |grep "Content-Type:"`;
</code></code>
Copy after login

http://stackoverflow.com/questions/2610713/get-mime-type-of-external-file-using-curl-and-php

你应该通过 HTTP 头部里面的 Content-Type 来判断数据类型。

CodeIgniter 提供了一份比较完整的 Content-Type(即 MIME Type)和扩展名的对应表,你可以参考一下。

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

heheheh 玻璃心犯了

高端逼们,玩好不送!!

<code>function online_filetype($url) {
    if (function_exists('curl_init'))
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $results = explode("\n", trim(curl_exec($ch)));
        foreach($results as $line) {
                if (strtok($line, ':') == 'Content-Type') {
                        $parts = explode(":", $line);
                        return trim($parts[1]);
                }
        }
    }else
    {
        $url = parse_url($url);
        if ($fp = @fsockopen($url['host'], empty($url['port']) ? 80 : $url['port'], $error)) {
            fputs($fp, "GET " . (empty($url['path']) ? '/' : $url['path']) . " HTTP/1.1\r\n");
            fputs($fp, "Host: {$url['host']}\r\n\r\n");
            while (!feof($fp)) {
                $tmp = fgets($fp);
                if (trim($tmp) == '') {
                    break;
                } else if (preg_match('/Content-Type:(.*)/si', $tmp, $arr)) {
                    return trim((string)$arr[1]);
                }
            }
            return null;
        } else {
            return null;
        }
    }
    return null;
}
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template