Make WordPress Core


Ignore:
Timestamp:
03/03/2025 12:17:08 AM (3 months ago)
Author:
joedolson
Message:

Media: Allow uploading images from URLs without extensions.

Enable download_url() to fetch and verify file types if the URL does not contain a file extension. This allows URL downloads to handle media endpoints like istockphoto.com that use file IDs and formatting arguments to deliver images.

Props masteradhoc, mitogh, joedolson, hellofromTonya, antpb, audrasjb, navi161, dmsnell.
Fixes #54738.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/file.php

    r59789 r59902  
    12421242    }
    12431243
     1244    $mime_type = wp_remote_retrieve_header( $response, 'content-type' );
     1245    if ( $mime_type && 'tmp' === pathinfo( $tmpfname, PATHINFO_EXTENSION ) ) {
     1246        $valid_mime_types = array_flip( get_allowed_mime_types() );
     1247        if ( ! empty( $valid_mime_types[ $mime_type ] ) ) {
     1248            $extensions     = explode( '|', $valid_mime_types[ $mime_type ] );
     1249            $new_image_name = substr( $tmpfname, 0, -4 ) . ".{$extensions[0]}";
     1250            if ( 0 === validate_file( $new_image_name ) ) {
     1251                if ( rename( $tmpfname, $new_image_name ) ) {
     1252                    $tmpfname = $new_image_name;
     1253                }
     1254
     1255                if ( ( $tmpfname !== $new_image_name ) && file_exists( $new_image_name ) ) {
     1256                    unlink( $new_image_name );
     1257                }
     1258            }
     1259        }
     1260    }
     1261
    12441262    $content_md5 = wp_remote_retrieve_header( $response, 'Content-MD5' );
    12451263
Note: See TracChangeset for help on using the changeset viewer.