Title: WP_Filesystem_Direct::put_contents
Published: April 25, 2014
Last modified: May 20, 2026

---

# WP_Filesystem_Direct::put_contents( string $file, string $contents, int|false $mode = false ): bool

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#wp--skip-link--target)

Writes a string to a file.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#parameters)󠁿

 `$file`stringrequired

Remote path to the file where to write the data.

`$contents`stringrequired

The data to write.

`$mode`int|falseoptional

The file permissions as octal number, usually 0644.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#return)󠁿

 bool True on success, false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#source)󠁿

    ```php
    public function put_contents( $file, $contents, $mode = false ) {
    	$fp = @fopen( $file, 'wb' );

    	if ( ! $fp ) {
    		return false;
    	}

    	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 );

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/class-wp-filesystem-direct.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/class-wp-filesystem-direct.php#L65)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/class-wp-filesystem-direct.php#L65-L89)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Filesystem_Direct::chmod()](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/chmod/)`wp-admin/includes/class-wp-filesystem-direct.php` |

Changes filesystem permissions.

  | 
| [mbstring_binary_safe_encoding()](https://developer.wordpress.org/reference/functions/mbstring_binary_safe_encoding/)`wp-includes/functions.php` |

Sets the mbstring internal encoding to a binary safe encoding when func_overload is enabled.

  | 
| [reset_mbstring_encoding()](https://developer.wordpress.org/reference/functions/reset_mbstring_encoding/)`wp-includes/functions.php` |

Resets the mbstring internal encoding to a users previously set encoding.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/put_contents/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_filesystem_direct%2Fput_contents%2F)
before being able to contribute a note or feedback.