Content · Programming · PHP

PHP file_put_contents() Function: Usage and Examples

Writes a string to a file, optionally with flags.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

file_put_contents() Function is one of the PHP operations used in filesystem and stream workflows. Writes a string to a file, optionally with flags. The examples below show what it does and how to handle its result.

What Is file_put_contents() Function?


Writes a string to a file, optionally with flags.

Its syntax is:

file_put_contents(string $filename, mixed $data, int $flags = 0, $context = null)

The parameters are:

  • string $filename — The source or destination filesystem path.
  • mixed $data — The bytes or value being written.
  • int $flags = 0 — Option constants that alter how the operation behaves.
  • $context = null — Optional PHP stream context controlling relevant stream or wrapper behavior.

Return value: The declared return type is int|false. By default it overwrites the file; LOCK_EX helps coordinate cooperating writers.

Important: By default it overwrites the file; LOCK_EX helps coordinate cooperating writers.

Example 1: Basic Usage


The basic example focuses on the behavior of file_put_contents() Function: writes a string to a file, optionally with flags. Inputs are defined locally or shown as application-controlled paths.

<?php

$filePath = sys_get_temp_dir() . "/demo-write.txt";
$bytesWritten = file_put_contents($filePath, "Ready\n", LOCK_EX);
var_dump($bytesWritten !== false);
unlink($filePath);

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for file_put_contents() Function. Notice the different checks, arguments, or output handling.

<?php

$path = sys_get_temp_dir() . "/append-demo.txt";
file_put_contents($path, "First\n");
file_put_contents($path, "Second\n", FILE_APPEND | LOCK_EX);
echo file_get_contents($path);
unlink($path);

This variation matters because by default it overwrites the file; LOCK_EX helps coordinate cooperating writers.

Practical Example: Using the Operation in an Application


The following example demonstrates how file_put_contents() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$logFile = sys_get_temp_dir() . "/batch-log.txt";
$written = file_put_contents($logFile, date("c") . " Task completed\n", FILE_APPEND | LOCK_EX);
if ($written === false) { throw new RuntimeException("Log write failed"); }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Writes a string to a file, optionally with flags.
  • Limitation: By default it overwrites the file; LOCK_EX helps coordinate cooperating writers.

Comparison With Related Functions


Function or MethodDifference
file_put_contents() FunctionWrites a string to a file, optionally with flags.
fwrite()Writes to an existing stream; file_put_contents() opens and closes the target internally.

Use file_put_contents() Function for the specific behavior described above. The related operation, fwrite(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Treating stream or file operations as guaranteed complete on the first call.By default it overwrites the file; LOCK_EX helps coordinate cooperating writers. Handle false returns, partial I/O, stream position and closing resources.
Treating fwrite() as an interchangeable replacement for file_put_contents() Function.Writes to an existing stream; file_put_contents() opens and closes the target internally.

Key Takeaways


  • Writes a string to a file, optionally with flags.
  • By default it overwrites the file; LOCK_EX helps coordinate cooperating writers.
  • Related operation: fwrite() — Writes to an existing stream; file_put_contents() opens and closes the target internally.

file_put_contents() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.