Get File Type by URL for a Remote File in WordPress

September 08, 2016

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


If the remote file is a png image with a url of, 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' );

Kellen Mace

Written by Kellen Mace, who lives in Rochester Hills, MI and builds cool stuff on the web. About Kellen // Follow him on Twitter →