- @fwrite($fp, $contents);
- @fclose($fp);
- $this->chmod($file, $mode);
+
+ mbstring_binary_safe_encoding();
+
+ $data_length = strlen( $contents );
+
+ $bytes_written = fwrite( $fp, $contents );
+
+ reset_mbstring_encoding();
+
+ fclose( $fp );
+
+ if ( $data_length !== $bytes_written )
+ return false;
+
+ $this->chmod( $file, $mode );
+