So i have a front end form that let users upload images from an URL...

Everything its ok when the image URL have the proper extension like this:


but when the image url doesn't have an extension like this :


My code doesn't work.

This is how i get the extension:

$filetype = wp_check_filetype($imageurl, null);

Now, if the file name of the image retrieved does have an extension, say jpg, then the output of the above call is

    [ext] => jpg
    [type] => image/jpeg

However if the extension is not part of the file name then the call above returns the following array

    [ext] =>
    [type] =>

How can i get the real file type for images that do not have an extension?

1 Answer 1


I think, you can not exactly determine, but you can assume one with header data.

$headers = wp_get_http_headers( $url );
$mime_type = $headers['content-type'];
$ext = '';
foreach ( wp_get_mime_types() as $exts => $mime ) {
    if ( $mime == $mime_type ) {
        $ext = $exts;

Now in the $ext variable, it could be multiple extensions that have been matched, and there's no way to determine which one the image actually have. But, you could try the first one.

if( $ext ){
    $exts = explode('|', $ext);
    $ext = $exts[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.