Backend Development 5 min read

Using PHP file_put_contents() to Write Data to Files

This article explains the PHP file_put_contents() function, detailing its syntax, parameters, return values, and providing multiple code examples for writing strings, appending data, handling arrays, and using callbacks to dynamically generate file content.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP file_put_contents() to Write Data to Files

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

The function syntax is:

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

Parameter description:

$filename : the target file name.

$data : the data to write, which can be a string, an array, or a callback that returns a string.

$flags : optional flags for additional write options; default is 0 (no options).

$context : optional stream context resource.

Return value description:

If the write succeeds, the function returns the number of bytes written.

If the write fails, it returns false .

Below are several concrete code examples demonstrating the use of file_put_contents() :

1. Write a string to a file:

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

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

2. Append a string to a file:

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

The FILE_APPEND flag appends the string to the existing file content.

3. Write an array to a file:

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

The array elements are joined with newline characters and written to the file.

4. Use a callback to write dynamic content (e.g., the current timestamp):

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

An anonymous function returns the current time, and its result is written to the file.

Additional usage ideas include combining file_put_contents() with other functions such as fopen() / fwrite() / fclose() or reading a file with file_get_contents() and then writing new content.

Summary

The file_put_contents() function is a convenient PHP file operation that simplifies writing data to files. It supports flexible parameters for strings, arrays, and callbacks, making it easy to write new content or append to existing files, thereby reducing code size and improving file‑write efficiency in backend development.

backendcode examplesfile handlingfile_put_contents
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

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