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 examples for writing strings, appending data, writing 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 is very useful for writing content to files efficiently without manually opening and closing them.

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, 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

On success, returns the number of bytes written.

On failure, returns false.

Using file_put_contents()

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

Appending a string to a file

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

Using the FILE_APPEND option appends the string "Hello, world again!" to test.txt .

Writing an array to a file

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

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

Beyond directly writing strings and arrays, you can use a callback function to generate content dynamically, 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>

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

The file_put_contents() function simplifies file writing in PHP, supporting flexible parameters for strings, arrays, and callbacks, and can handle both new writes and appends, reducing code size and improving efficiency.

PHP Learning Recommendations

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Expert Recommended Course

Workerman+TP6 Real‑time Chat System – Limited Time Offer

BackendPHPCode ExampleTutorialfile 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.