Title: WP_Filesystem_ftpsockets::put_contents
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

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

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

Writes a string to a file.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_filesystem_ftpsockets/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_ftpsockets/put_contents/?output_format=md#return)󠁿

 bool True on success, false on failure.

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

    ```php
    public function put_contents( $file, $contents, $mode = false ) {
    	$tempfile   = wp_tempnam( $file );
    	$temphandle = @fopen( $tempfile, 'w+' );

    	if ( ! $temphandle ) {
    		unlink( $tempfile );
    		return false;
    	}

    	// The FTP class uses string functions internally during file download/upload.
    	mbstring_binary_safe_encoding();

    	$bytes_written = fwrite( $temphandle, $contents );

    	if ( false === $bytes_written || strlen( $contents ) !== $bytes_written ) {
    		fclose( $temphandle );
    		unlink( $tempfile );

    		reset_mbstring_encoding();

    		return false;
    	}

    	fseek( $temphandle, 0 ); // Skip back to the start of the file being written to.

    	$ret = (bool) $this->ftp->fput( $file, $temphandle );

    	reset_mbstring_encoding();

    	fclose( $temphandle );
    	unlink( $tempfile );

    	$this->chmod( $file, $mode );

    	return $ret;
    }
    ```

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

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

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

Changes filesystem permissions.

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

Returns a filename of a temporary unique file.

  | 
| [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.

  |

| Used by | Description | 
| [WP_Filesystem_ftpsockets::copy()](https://developer.wordpress.org/reference/classes/wp_filesystem_ftpsockets/copy/)`wp-admin/includes/class-wp-filesystem-ftpsockets.php` |

Copies a file.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_filesystem_ftpsockets/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_ftpsockets%2Fput_contents%2F)
before being able to contribute a note or feedback.