Search code examples
phpimagecurl

php/curl - get extension of image on remote server using headers


There is a form on my website that allows the upload of images from a remote server. When the user enters the link and submits, I want to check the file and make sure it's the right extension before I copy it off to my server.

I tried to directly use exif_imagetype, but allow_url_fopen is not allowed on the server so need help. I think using curl will solve the problem, but I'm not sure how to get the image extension from the header.


solution

$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec ($ch);
                        
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo $content_type;

Thanks to Mario!


Solution

  • solution

    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_exec ($ch);
    
    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    echo $content_type;
    

    Thanks to Mario!