The file_put_contents() function writes a string to a file. The function returns the number of bytes that were written to the file, or FALSE on failure.
file_put_contents(file_path, data, flags, context)
file_path − The path of the file.
data − Set the data to be written in the file:
flags − Specifies how the file is opened or data has to be written:
FILE_USE_INCLUDE_PATH − Search for filename in the include directory.
FILE_APPEND − If file filename already exists, append the data to the file instead of overwriting it.
LOCK_EX − Acquire an exclusive lock on the file while proceeding to the writing.
FILE_TEXT − data is written in text mode. This flag cannot be used with FILE_BINARY. This flag is only available since PHP 6.
FILE_BINARY − data will be written in binary mode. This is the default setting and cannot be used with FILE_TEXT. This flag is only available since PHP 6.
context − Set the behaviour of the stream.
The file_put_contents() function returns the number of bytes that were written to the file, or FALSE on failure.
<?php echo file_put_contents("new.txt","This is it!"); ?>
Let us see another example −
<?php $file_path = one.txt'; $myfile = file_get_contents($file_path); // Append $myfile .= "Demo line!\n"; file_put_contents($file_path, $myfile); ?>