Reputation: 825
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.
Thanks in advanced!
$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!
Upvotes: 2
Views: 3715
Reputation: 2550
You can also do it this way:
$src = 'https://example.com/image.jpg';
$size = getimagesize($src);
$extension = image_type_to_extension($size[2]);
Upvotes: 0
Reputation: 1
Based on a few responses, I was trying to display an image from a curl request, and dynamically figure out the headers Content-Type. Using what Mario did, and some other things, here is a function which will correctly display an image from a standard url or file, and correctly put in the header. All PHP.
function displayImage( $filename ) {
$ch = curl_init();
$options = array(
CURLOPT_URL => $filename,
CURLOPT_ENCODING => "",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
CURLOPT_CONNECTTIMEOUT => 60,
CURLOPT_TIMEOUT => 60
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
header('Content-Type: ' . curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
if(!curl_errno($ch)){
curl_close($ch);
$img = imagecreatefromstring($response);
imagejpeg($img);
imagedestroy($img);
return true;
}
else{
curl_close($ch);
return curl_error($ch);
}
}
Upvotes: 0
Reputation: 825
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!
Upvotes: 6