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

 网吧b国漫救星 发布于 2022-12-01 18:52

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

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

7 个回答
  • 2022-12-01 19:18 回答
  • 大家写的代码很不高效呀

    <?php
    echo `curl -Is 'http://s11.sinaimg.cn/mw690/e0571d75tx6Co4vJcUOfa&690' |grep "Content-Type:"`;
    
    2022-12-01 19:18 回答
  • 2022-12-01 19:18 回答
  • 你应该通过 HTTP 头部里面的 Content-Type 来判断数据类型。

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

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

    2022-12-01 19:18 回答
  • heheheh 玻璃心犯了

    高端逼们,玩好不送!!

    2022-12-01 19:18 回答
  • 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;
    }
    
    2022-12-01 19:18 回答
  • /
    //方法一
    function get_extension($file)
    {
        // strrchr函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符
        $file = explode('?',$file)[0];
        return substr(strrchr($file, '.'), 1);
    }
    echo get_extension($url);
    /

    /
    //方式二
    function get_extension($file)
    {
    //strrpos,最后一次出现的位置
        $file = explode('?',$file)[0];
        return substr($file, strrpos($file, '.')+1);
    }     
    echo get_extension($url);
    /

    /
    //方式三
    function get_extension($file)
    {
        //end  将数组的内部指针指向最后一个单元
        $file = explode('?',$file)[0];
        return end(explode('.', $file));
    }
    echo get_extension($url);
    /

    /
    //方式三
    function get_extension($file)
    {
        $file = explode('?',$file)[0];
        $info = pathinfo($file);
        return $info['extension'];
    }
    echo get_extension($url);
    /

     

    2022-12-01 21:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有