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

php curl get response headers
php curl get request with headers
php curl response headers array
php get response headers
php curl post
get curl response code php
php curl header
php curl example

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!


$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!

curl_getinfo - Manual, curl_getinfo — Get information regarding a specific transfer CURLINFO_FILETIME - Remote time of the retrieved document, with the CURLOPT_FILETIME NULL indicates server did not send valid Content-Type: header; CURLINFO_PRIVATE - Private data associated with 13: format error in certificate's notBefore field For this example we use three files uploader.php -that receives the files on to the remote server. handler.php -is used to web based client to upload the file. curl_handler.php -PHP cURL uploading client to upload file. Reply Delete

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_HTTPGET => true,
        CURLOPT_TIMEOUT => 60


    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);
    header('Content-Type: ' . curl_getinfo($ch, CURLINFO_CONTENT_TYPE));

        $img = imagecreatefromstring($response);
        return true;
        return curl_error($ch);

cURL - Manual, The second argument is optional, and it allows you to check for a specific response In this example: by "​frank at I needed to use cURL in a php script to download data using not only SSL for the server $this->headers[] = 'Accept: image/gif, image/x-bitmap, image​/jpeg,  Next, we will need to cut out the headers. Using this method, we can return both the body and header part of the response. However, before we can separate the components of the response, we need to get the size of the header. This can be done using curl_getinfo() with the CURLINFO_HEADER_SIZE option, as shown below:

You can also do it this way:

$src = '';
$size = getimagesize($src);
$extension = image_type_to_extension($size[2]);

PHP Object-Oriented Solutions, One of PHP's great strengths is its ability to access files on other servers, allow_url_fopen was modified in PHP 5.2 to prevent direct inclusion of remote files, and a The PHP cURL (Client URL Library) extension provides an alternative way to It's a nuisance to have to check which method is available, particularly if you  This class can send HTTP requests to remote servers using Curl. It can configure a request to be sent by setting the remote server URL and the HTTP method. Additional parameters may also be configured passing an array with key value pairs for parameters like user name and password for authentication, connection and data transfer timeout, referer header and any arbitrary HTTP request headers.

get_headers - Manual, get_headers — Fetches all the headers sent by the server in response to an HTTP request get_headers ( string $url [, int $format = 0 [, resource $context ]] ) : array Instead, use cURL functions to get headers for a URL provided by the user and We can use get_headers to check image url is real image resource or not This class can Send HTTP request to remote servers using PHP Curl extension. It can send an HTTP GET or POST request with custom parameters to a given remote URL. The class also supports options to configure the HTTP requests like handling redirects, response timeout period, referrer URL, whether it should return the response data, request headers, upload files, and proxy server to use.

PHP: Get the size of a remote file., Content-Length is an optional header for servers, which means that it might not If you would like to use PHP's cURL extension to get the size of a remote file,  This option causes curl to save the retrieved file with the same name that the file has on the remote server. The -n 1 option tells xargs to treat each line of the text file as a single parameter. When you run the command, you’ll see multiple downloads start and finish, one after the other.

How To Use, You can specify a step counter for the ranges to get every Nth number or letter: (HTTP) Tells curl to use HTTP Basic authentication with the remote host. The file format of the file to read cookies from should be plain HTTP headers Example: send an image to an HTTP server, where 'profile' is the name of the form-field  PHP: Set custom headers with cURL. This is a guide on how to set custom request headers with PHP’s cuRL extension. Using cURL’s CURLOPT_HTTPHEADER option, we can change request headers such as Referer, Cookie, User-Agent and Accept-Encoding.

  • Headers don't contain extensions. Do you mean the MIME type perhaps?
  • possible duplicate of PHP curl_getinfo($ch, CURLINFO_CONTENT_TYPE) returns no charset
  • image/jpeg - these types
  • Yes, that's the mime types. Use curl_getinfo() as in that other example.