- fwrite($temp, $contents);
- fseek($temp, 0); //Skip back to the start of the file being written to
- $ret = @ftp_fput($this->link, $file, $temp, $type);
+
+ mbstring_binary_safe_encoding();
+
+ $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 );
+