1

I am using download_url() to download a temporary file and want to save this file in my uploads folder using media_handle_sideload. This works fine with files that have a file extension.

media_handle_sideload dosent work with files that have no file extension. So when im trying to save for example this image it wont save, because the url has no file extension.

Then I need to use image_type_to_extension(exif_imagetype($imageurl)); to get the file extension for the image. How can I add this file extension to the temporary file?

here is my code:

public function upload_image_from_url($imageurl) {

        // Get the file extension for the image
        $fileextension = image_type_to_extension(exif_imagetype($imageurl));

        // Save as a temporary file
        $tmp = download_url( $imageurl );

        $file_array = array(
            'name' => basename( $imageurl ),
            'tmp_name' => $tmp
        );

        // Check for download errors
        if ( is_wp_error( $tmp ) ) {
            @unlink( $file_array[ 'tmp_name' ] );
            return $tmp;
        }

        $id = media_handle_sideload( $file_array, 0 );
        // Check for handle sideload errors.
        if ( is_wp_error( $id ) ) {
            @unlink( $file_array['tmp_name'] );
            return $id;
        }

        $attachment_url = wp_get_attachment_url( $id );


        return $attachment_url;

}    

Thanks.

1 Answer 1

3

The problem:

When you download an image with an extension, for example jsbach_image.jpg, it will look like this in the temp directory:

/tmp/jsbach.tmp

but when you download an image without an extension, for example mozart_image, you will get:

/tmp/mozart 

But the latter one will not pass the filetype- and extension tests in the wp_handle_sideload() function, called by the media_handle_sideload() function.

A possible solution:

We can trick it to pass the test, by renaming (or copying) the temp file with an extension.

You can try the following modifications:

/**
 * Upload an image from an url, with support for filenames without an extension
 * 
 * @link   http://wordpress.stackexchange.com/a/145349/26350
 * @param  string $imageurl
 * @return string $attachment_url
 */
function upload_image_from_url( $imageurl )
{
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    // Get the file extension for the image
    $fileextension = image_type_to_extension( exif_imagetype( $imageurl ) );

    // Save as a temporary file
    $tmp = download_url( $imageurl );

    // Check for download errors
    if ( is_wp_error( $tmp ) ) 
    {
        @unlink( $file_array[ 'tmp_name' ] );
        return $tmp;
    }

    // Image base name:
    $name = basename( $imageurl );

    // Take care of image files without extension:
    $path = pathinfo( $tmp );
    if( ! isset( $path['extension'] ) ):
        $tmpnew = $tmp . '.tmp';
        if( ! rename( $tmp, $tmpnew ) ):
            return '';
        else:
            $ext  = pathinfo( $imageurl, PATHINFO_EXTENSION );
            $name = pathinfo( $imageurl, PATHINFO_FILENAME )  . $fileextension;
            $tmp = $tmpnew;
        endif;
    endif;

    // Upload the image into the WordPress Media Library:
    $file_array = array(
        'name'     => $name,
        'tmp_name' => $tmp
    );
    $id = media_handle_sideload( $file_array, 0 );

    // Check for handle sideload errors:
    if ( is_wp_error( $id ) )
    {
        @unlink( $file_array['tmp_name'] );
        return $id;
    }

    // Get the attachment url:
    $attachment_url = wp_get_attachment_url( $id );

    return $attachment_url;
}  

I hope this helps.

3
  • Glad to hear that, I hope you can modify this demo to your needs.
    – birgire
    Commented May 23, 2014 at 11:58
  • This didn't work for me. I had to check the extension on $imageurl instead, and if that's not present, do the name change. I was getting .tmp always even with files without extension. Commented Jun 8, 2015 at 21:43
  • @AlainJacometForte thanks for your feedback. Did something change in WordPress since this was posted last year, or is this something specific to your install/environment?
    – birgire
    Commented Jun 9, 2015 at 13:27

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.