PHP从url查找图像的扩展名,没有扩展名

Mat*_*mik 2 php

我有一个这样的网址:

$file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i215";
Run Code Online (Sandbox Code Playgroud)

它可以是图像或视频,但它在网址中没有扩展名.

如何从URL获取此文件的扩展名,以便我可以使用正确的扩展名保存它?

$extension=?;    
copy($file,"newname.$extension");
Run Code Online (Sandbox Code Playgroud)

sum*_*mit 5

您可以使用

$file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i215";
//copy file to server

$type= mime_content_type($file)
//rename file based on the type
Run Code Online (Sandbox Code Playgroud)

产量

image/gif
Run Code Online (Sandbox Code Playgroud)

http://php.net/manual/en/function.mime-content-type.php