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.