PHP从url查找图像的扩展名,不带扩展名
我有这样一个url:PHP从url查找图像的扩展名,不带扩展名,php,Php,我有这样一个url: $file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i215"; 它将是图像或视频,但在url中没有扩展名 如何从url获取此文件的扩展名,以便使用正确的扩展名保存它 $extension=?; copy($file,"newname.$extension"); 你可以用 $file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i2
$file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i215";
它将是图像或视频,但在url中没有扩展名
如何从url获取此文件的扩展名,以便使用正确的扩展名保存它
$extension=?;
copy($file,"newname.$extension");
你可以用
$file="http://example.com/307i215/sn9gyyzpry09m3e97z6hhbnw84i215";
//copy file to server
$type= mime_content_type($file)
//rename file based on the type
输出
image/gif
我找到了另一个解决方案供您参考:
$extension=get_headers($file,1)["Content-Type"];
复制前将输出
image/gif
。您试过了吗?哦,好的!我必须用扩展名保存文件吗?如果我不这么做有关系吗?或者我应该在找到内容类型后重命名它?最好保存并找到文件类型,然后重新命名