Backend Development 5 min read

Using PHP file_put_contents() to Write Data to Files

This article explains the PHP file_put_contents() function, its syntax, parameters, return values, and provides practical code examples for writing strings, appending data, handling arrays, and using callbacks to efficiently manage 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, and the file_put_contents() function offers a simple and efficient way to write data to a file without manually opening and closing it.

The syntax of file_put_contents() is:

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

Parameter description:

$filename : the target file name to write to.

$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 extra options).

$context : optional stream context resource.

Return value description:

If the write succeeds, the number of bytes written is returned.

If the write fails, false is returned.

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));

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

4. Write data generated by a callback function:

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

When using file_put_contents() , you can also combine it with other functions such as fopen() + fwrite() + fclose() , or read a file with file_get_contents() and then write new content with file_put_contents() .

Summary

The file_put_contents() function is a convenient PHP file‑handling utility that simplifies writing data to files. It supports flexible parameters for strings, arrays, and callbacks, and can handle both new writes and appends, reducing code volume and improving file‑write efficiency in backend development.

BackendPHPfile handlingfile writefile_put_contentsphp tutorial
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.