PHP file_put_contents() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, file_put_contents() writes data to a file in a single call. It creates the file if it does not exist and, by default, replaces the old content.

It is the easiest way to save text, logs or JSON. You do not need fopen(), fwrite() and fclose(). Add the FILE_APPEND flag when you want to add to the end of a file instead of overwriting it.

Syntax

Call file_put_contents() with the values you need. Start with this line.

PHP
$bytes = file_put_contents('data.txt', 'Hello');
CallWhat it does
file_put_contents('data.txt', 'Hello')Creates or overwrites data.txt and returns the number of bytes written.
file_put_contents('log.txt', $line, FILE_APPEND)Adds the text to the end of the file.
file_put_contents('data.txt', $text, LOCK_EX)Locks the file while writing, so two scripts do not write at once.

The data can be a string, an array or a stream resource. An array is joined together like implode('', $array). The function overwrites existing files unless you add FILE_APPEND.

Library declarations
PHP
file_put_contents(string $filename, mixed $data, int $flags = 0, ?resource $context = null): int|false

Parameters

ParameterDescription
filenameRequired. The path of the file to write. The folder must already exist.
dataRequired. A string, an array of strings or a stream resource. Other values are converted to a string.
flagsOptional. FILE_USE_INCLUDE_PATH, FILE_APPEND and LOCK_EX, combined with the | operator.
contextOptional. A stream context created with stream_context_create(). Use null for the default.

Return Value

Return ValueDescription
intThe number of bytes written to the file.
falseReturned when the file cannot be written.

PHP Examples

Example 1 — Write Text to a File

Save a short message and print how many bytes were written.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fpc.txt';

$bytes = file_put_contents($path, 'Hello');

echo 'Wrote ', $bytes, ' bytes', PHP_EOL;
echo 'File contains: ', file_get_contents($path), PHP_EOL;

unlink($path);

How It Works

  1. file_put_contents() creates the file because it does not exist yet.
  2. It returns 5, the number of bytes in the word Hello.
  3. file_get_contents() reads the file back to confirm the content.
  4. unlink() deletes the demo file.

Example 2 — Writing to a Folder That Does Not Exist

PHP will not create missing folders for you. The call fails and returns false.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_no_such_folder/test.txt';

$result = @file_put_contents($path, 'Hello');

if ($result === false) {
    echo 'Could not write the file', PHP_EOL;
} else {
    echo 'Wrote ', $result, ' bytes', PHP_EOL;
}

How It Works

  1. The folder codetofun_no_such_folder does not exist.
  2. file_put_contents() cannot create the file inside it and returns false.
  3. The @ operator hides the warning so the script can react itself.
  4. Use mkdir() first when you need to create the folder.

Example 3 — Append Lines with FILE_APPEND

Add the FILE_APPEND flag to keep the old content and add new text at the end, like a simple log file.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fpc_append.txt';
@unlink($path);

file_put_contents($path, 'Line 1' . PHP_EOL);
file_put_contents($path, 'Line 2' . PHP_EOL, FILE_APPEND);

echo file_get_contents($path);

unlink($path);

How It Works

  1. The first call creates the file and writes Line 1.
  2. The second call uses FILE_APPEND, so Line 2 is added at the end.
  3. Without FILE_APPEND, the second call would erase Line 1.
  4. file_get_contents() shows both lines in the file.

Frequently Asked Questions

It writes data to a file in one step. It creates the file if needed and replaces the old content, unless you use FILE_APPEND.
Pass FILE_APPEND as the third argument. You can add LOCK_EX with the | operator to prevent two scripts from writing at the same time.
Yes. The strings in the array are joined together with nothing between them, like implode('', $array). Add your own line breaks if you need them.
The folder may not exist, PHP may not have permission to write there, or the disk may be full. Check the warning and the folder permissions.
file_put_contents() opens, writes and closes the file in one call. fwrite() writes to a file you opened yourself, which is better when you write many times.

Next: fileatime()

fileatime() returns the last time a file was accessed (read), as a Unix timestamp. A Unix .

fileatime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful