Mastering PHP’s file_put_contents(): Write, Append, and Stream Data Efficiently

This guide explains PHP’s file_put_contents function, covering its syntax, parameters, return values, and practical examples for writing strings, appending data, handling arrays, and using callbacks, while also offering tips for combining it with other file functions for flexible file manipulation.

php Courses
php Courses
php Courses
Mastering PHP’s file_put_contents(): Write, Append, and Stream Data Efficiently

In PHP development, handling files is common, and the file_put_contents() function provides a simple, efficient way to write data without manually opening and closing files.

Syntax

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

Parameter description

$filename

: target file name. $data: data to write; can be a string, array, or a callback returning a string. $flags: optional flags, default 0 (no extra options). $context: optional stream context resource.

Return value

On success, returns the number of bytes written.

On failure, returns false.

Usage examples

Write a string

$filename = 'test.txt';
$data = 'Hello, world!';
file_put_contents($filename, $data);

This creates test.txt and writes the string "Hello, world!" into it.

Append a string

$filename = 'test.txt';
$data = 'Hello, world again!';
file_put_contents($filename, $data, FILE_APPEND);

Using the FILE_APPEND flag appends the string to the existing file.

Write an array

$filename = 'test.txt';
$data = ['apple', 'banana', 'orange'];
file_put_contents($filename, implode("
", $data));

The array elements are joined with newline characters and written to test.txt.

Write using a callback

$filename = 'test.txt';
$data = function() {
    return date('Y-m-d H:i:s');
};
file_put_contents($filename, $data());

An anonymous function returns the current timestamp, which is then written to the file.

Additional tips

Combine with fopen(), fwrite(), and fclose() for finer control.

Read existing content with file_get_contents() and then write new data using file_put_contents().

The file_put_contents() function simplifies file operations in PHP, handling strings, arrays, and callbacks, and supports both overwriting and appending with minimal code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPfile-handlingfile_put_contentsphp-tutorialfile writing
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.