简体   繁体   中英

PHP Find extension of image from url with no extension

I have a url like so:

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

It will either be an image or a video but it does not have the extension within the url.

How can I get the extension of this file from the url so that I can save it with the right extension?

$extension=?;    
copy($file,"newname.$extension");

You can use

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

$type= mime_content_type($file)
//rename file based on the type

output

image/gif

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

I found another solution for your information:

$extension=get_headers($file,1)["Content-Type"];

Will output image/gif before copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM