How to safely get the file extension from a URL?

后端 未结 9 1334
北荒
北荒 2021-02-02 08:40

Consider the following URLs

http://m3u.com/tunein.m3u
http://asxsomeurl.com/listen.asx:8024
http://www.plssomeotherurl.com/station.pls?id=111
http://22.198.133.16:802         


        
9条回答
  •  面向向阳花
    2021-02-02 09:20

    The real proper way is to not use file extensions at all. Do a GET (or HEAD) request to the URL in question, and use the returned "Content-type" HTTP header to get the content type. File extensions are unreliable.

    See MIME types (IANA media types) for more information and a list of useful MIME types.

提交回复
热议问题