Sep 8, 2016

Get File Type by URL for a Remote File in WordPress

If you want to get the file mime type for a remote file and you have its URL, the function below can be used.

Example

If the remote file is a png image with a url of http://example.com/directory/image.png?ver=123, passing in that URL to this function would return image/png.

/**
 * Get the file mime type for a file by its URL.
 *
 * @param  $url The URL to the file.
 * @return      The file mime type or empty string on failure.
 */
function km_get_file_type_by_url( $url ) {
	$response  = wp_remote_get( $url );

	return wp_remote_retrieve_header( $response, 'content-type' );
}