0

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:

http://mydomain.com/image.jpg

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

http://mydomain.com/image

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

Array
(
    [ext] => jpg
    [type] => image/jpeg
)

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

Array
(
    [ext] =>
    [type] =>
)

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

2

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;
        break;
    }
}

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];
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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