Make WordPress Core


Ignore:
Timestamp:
02/27/2017 03:38:30 PM (8 years ago)
Author:
joemcgill
Message:

Media: Keep PDF previews from overwriting files.

Since support for PDF previews were added in [38949], it's possible
that the generated image file could overwrite an existing image file
with the same name. This uses wp_unique_filename() to avoid this
issue and adds a '-pdf' identifier on the end of filenames.

Props gitlost, derosj, mikeschroder, joemcgill.
Fixes #39875. See #31050.

File:
1 edited

Legend:

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

    r39617 r40130  
    252252
    253253            if ( ! is_wp_error( $editor ) ) { // No support for this type of file
    254                 $uploaded = $editor->save( $file, 'image/jpeg' );
     254                /*
     255                 * PDFs may have the same file filename as JPEGs.
     256                 * Ensure the PDF preview image does not overwrite any JPEG images that already exist.
     257                 */
     258                $dirname = dirname( $file ) . '/';
     259                $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION );
     260                $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' );
     261
     262                $uploaded = $editor->save( $preview_file, 'image/jpeg' );
    255263                unset( $editor );
    256264
Note: See TracChangeset for help on using the changeset viewer.