# file_put_contents() function in PHP

PHPProgrammingServer Side Programming

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.

## Syntax

file_put_contents(file_path, data, flags, context)

## Parameters

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

## Return

The file_put_contents() function returns the number of bytes that were written to the file, or FALSE on failure.

## Example

Live Demo

<?php
echo file_put_contents("new.txt","This is it!");
?>

## Output

11

Let us see another example −

## Example

<?php
$file_path = one.txt';$myfile = file_get_contents($file_path); // Append$myfile .= "Demo line!\n";
file_put_contents($file_path,$myfile);
?>

## Output

10
Published on 03-Oct-2018 15:19:12