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, while also offering tips for combining it with other file functions.

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 writing content to files without needing to open and close them manually.

The syntax of file_put_contents() is as follows:

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 concrete code examples demonstrating how to use 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 .

In addition to writing strings and arrays, you can use a callback function to generate content dynamically, such as writing the current time:

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

The 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, for example:

Open a file with fopen() , write with fwrite() , and close with fclose() .

Read a file with file_get_contents() and then write new content with file_put_contents() .

Summary

The file_put_contents() function is a very convenient file operation function in PHP that simplifies the file‑writing process. It offers flexible parameters to handle strings, arrays, and callbacks, and can easily write new content or append to existing files, reducing code volume and improving write efficiency in real‑world development.

PHP Practical Development Fast‑Track Introduction

Scan the QR code to receive free learning materials

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.