Backend Development 4 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 multiple code examples for writing strings, appending data, and handling arrays or callbacks to efficiently write content to files without manual opening and closing.

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 is very useful for file operations, allowing you to write content to a file in a simple and efficient way without opening and closing the file manually.

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 : the target file name to write to.

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

$flags : optional flags for additional write options; default 0 means 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 to a file

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

The above code creates a file named test.txt in the current directory and writes the string "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>

This uses the FILE_APPEND flag to append the string to test.txt .

Write an array to a file

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

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

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

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

This uses an anonymous function to return the current timestamp and writes it to the file.

In practice, file_put_contents() can be combined with other functions like fopen() , fwrite() , fclose() , or file_get_contents() to achieve more complex file manipulation.

The file_put_contents() function simplifies file writing in PHP, supports flexible parameters, and can handle strings, arrays, and callbacks, reducing code and improving efficiency.

Backend Developmentphpcode examplesfile handlingfile_put_contentswriting files
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.