Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从url查找图像的扩展名,不带扩展名_Php - Fatal编程技术网

PHP从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

我有这样一个url:

$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

您试过了吗?哦,好的!我必须用扩展名保存文件吗?如果我不这么做有关系吗?或者我应该在找到内容类型后重命名它?最好保存并找到文件类型,然后重新命名