Backend Development 5 min read

Using PHP file_put_contents() to Write Data to Files

This article explains the PHP file_put_contents() function, covering its syntax, parameters, return values, and multiple usage examples such as writing strings, appending data, handling arrays, and using callbacks, while also offering tips for efficient file operations in backend development.

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. The file_put_contents() function provides a simple and efficient way to write data to a file without manually opening and closing it.

Syntax of file_put_contents()

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

Parameter description

$filename : target file name.

$data : data to write; can be a string, an array, or a callback returning a string.

$flags : optional flags, default 0 (no extra options).

$context : optional stream context resource.

Return values

On success, returns the number of bytes written.

On failure, returns false.

Examples of using file_put_contents()

Write a string to a file

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

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

Append a string to a file

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

The FILE_APPEND flag appends the string to test.txt instead of overwriting it.

Write an array to a file

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

This joins the array elements with newline characters and writes them to test.txt .

You can also pass a callback function to generate content dynamically, such as writing the current timestamp:

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

The anonymous function returns the current time, which is then written to the file.

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

Overall, file_put_contents() is a convenient PHP function that reduces code size and improves file‑writing efficiency, supporting strings, arrays, and callbacks for both new writes and appends.

The article also lists several recommended PHP learning resources for further study.

backend developmentphpcode 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.