From ff6d8819e421e1cbcb13f92a4e49a0d2f3647cf8 Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Mon, 18 Jul 2016 19:42:48 +0000 Subject: [PATCH] Filesystem API: Cleanup temporary file when the temporary file couldn't be opened. Props ruud@joyo. See #34772. Fixes #36942, #36943. git-svn-id: https://develop.svn.wordpress.org/trunk@38094 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-wp-filesystem-ftpext.php | 9 ++++++--- src/wp-admin/includes/class-wp-filesystem-ftpsockets.php | 4 +++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/includes/class-wp-filesystem-ftpext.php b/src/wp-admin/includes/class-wp-filesystem-ftpext.php index e1202aedd2..f337c733c8 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ftpext.php +++ b/src/wp-admin/includes/class-wp-filesystem-ftpext.php @@ -120,13 +120,13 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { unlink( $tempfile ); return false; } - + if ( ! @ftp_fget( $this->link, $temp, $file, FTP_BINARY ) ) { fclose( $temp ); unlink( $tempfile ); return false; } - + fseek( $temp, 0 ); // Skip back to the start of the file being written to $contents = ''; @@ -159,8 +159,11 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { public function put_contents($file, $contents, $mode = false ) { $tempfile = wp_tempnam($file); $temp = fopen( $tempfile, 'wb+' ); - if ( ! $temp ) + + if ( ! $temp ) { + unlink( $tempfile ); return false; + } mbstring_binary_safe_encoding(); diff --git a/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php b/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php index fe74d33c1d..ff0b882414 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php +++ b/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php @@ -121,8 +121,10 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { $temp = wp_tempnam( $file ); - if ( ! $temphandle = fopen($temp, 'w+') ) + if ( ! $temphandle = fopen( $temp, 'w+' ) ) { + unlink( $temp ); return false; + } mbstring_binary_safe_encoding();