- $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
- $ret = @ftp_fput($this->link, $file, $temp, $type);
+ $data_length = strlen( $contents );
+ $bytes_written = fwrite( $temp, $contents );
+
+ reset_mbstring_encoding();
+
+ if ( $data_length !== $bytes_written ) {
+ fclose( $temp );
+ unlink( $tempfile );
+ return false;
+ }
+
+ fseek( $temp, 0 ); // Skip back to the start of the file being written to
+
+ $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );