WP_Filesystem_FTPext::put_contents() WordPress Method

The put_contents() method is part of the WP_Filesystem_FTPext class, which is used to manage and manipulate files on a FTP server. This particular method is used to write a given string of data to a specified file on the server. The first parameter is the path to the file on the server where the data will be written, and the second parameter is the string of data to be written. If the file already exists, it will be overwritten with the new data. Otherwise, a new file will be created.

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

Writes a string to a file.


Parameters

$file

(string)(Required)Remote path to the file where to write the data.

$contents

(string)(Required)The data to write.

$mode

(int|false)(Optional) The file permissions as octal number, usually 0644.

Default value: false


Top ↑

Return

(bool) True on success, false on failure.


Top ↑

Source

File: wp-admin/includes/class-wp-filesystem-ftpext.php

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

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

		mbstring_binary_safe_encoding();

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

		reset_mbstring_encoding();

		if ( $data_length !== $bytes_written ) {
			fclose( $temphandle );
			unlink( $tempfile );
			return false;
		}

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

		$ret = ftp_fput( $this->link, $file, $temphandle, FTP_BINARY );

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

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

		return $ret;
	}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.