Master PHP file_put_contents: Write Strings, Arrays, and Use Flags

Learn how to use PHP's file_put_contents() function to write strings and arrays to files, explore flag options like FILE_APPEND and LOCK_EX, and handle context resources, with clear code examples demonstrating each scenario.

php Courses
php Courses
php Courses
Master PHP file_put_contents: Write Strings, Arrays, and Use Flags

In PHP we often need to write data to a file; the built‑in file_put_contents() function handles this.

Function syntax

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

The function accepts four parameters: the filename, the data to write, optional flags, and an optional context resource.

Writing a string to a file

Example of writing the string “Hello, world!” to test.txt:

$file = "test.txt";
$data = "Hello, world!";
file_put_contents($file, $data);

Running this creates test.txt in the current directory with the content “Hello, world!”.

Writing an array to a file

To store an array, we first convert it to a string with var_export() and then write it:

$file = "users.txt";
$data = array(
    array("name" => "John", "age" => 25),
    array("name" => "Emma", "age" => 28),
    array("name" => "Michael", "age" => 31)
);
file_put_contents($file, var_export($data, true));

The resulting users.txt contains a PHP‑style array representation of the data.

Setting flags

The third parameter $flags controls how data is written. Common flags include: FILE_USE_INCLUDE_PATH: search the include_path for the file. FILE_APPEND: append data to the end of the file instead of overwriting. LOCK_EX: acquire an exclusive lock while writing to prevent concurrent writes.

Example of appending a log entry using FILE_APPEND:

$file = "log.txt";
$data = "New log entry";
file_put_contents($file, $data, FILE_APPEND);

After execution, log.txt contains the new entry.

Context parameter

The fourth parameter is a context resource for advanced file operations; consult the PHP manual for details.

Summary

Using file_put_contents() you can easily write strings or arrays to files, customize behavior with flags, and employ a context resource for more complex scenarios.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode 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

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.