Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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];
}
share|improve this answer
    
Thank you Shazzad! It works. –  Oscar Dec 15 '13 at 1:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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