Backend Development 4 min read

Using PHP file_put_contents() to Write and Append Data to Files

This article explains the PHP file_put_contents() function, covering its syntax, parameters, return values, and provides clear code examples for both overwriting and appending data to files, helping developers efficiently handle file writing tasks in backend applications.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP file_put_contents() to Write and Append Data to Files

In PHP development, the file_put_contents() function provides a convenient way to write strings, arrays, or other data to a file. This guide introduces its usage, parameters, return values, and includes practical code examples.

Basic Syntax

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

Parameter Description

$filename : The target file name, which can be a relative or absolute path.

$data : The data to write; it may be a string, array, or other type.

$flags (optional): Flags that modify the write behavior, such as FILE_APPEND for appending and LOCK_EX for an exclusive lock.

$context (optional): A stream context resource.

Return Value

On success, the number of bytes written is returned.

On failure, false is returned.

Writing Data to a File

The following example demonstrates how to write a string to a file using file_put_contents() and check the result.

<code>&lt;?php
$filename = "data.txt";
$data = "Hello, World!";

// Write the string to the file
$result = file_put_contents($filename, $data);

if ($result !== false) {
    echo "Data successfully written, total bytes: " . $result;
} else {
    echo "Failed to write to file!";
}
?&gt;</code>

Appending Data to a File

To append data instead of overwriting, include the FILE_APPEND flag in the $flags parameter.

<code>&lt;?php
$filename = "data.txt";
$data = "Hello, World!";

// Append data to the file
$result = file_put_contents($filename, $data, FILE_APPEND);

if ($result !== false) {
    echo "Data successfully appended, total bytes added: " . $result;
} else {
    echo "Failed to append to file!";
}
?&gt;</code>

In both examples, the return value is examined to determine whether the operation succeeded.

Conclusion

This guide has covered the usage of the PHP file_put_contents() function, including its syntax, parameters, return values, and practical examples for both overwriting and appending data. By applying these techniques, developers can efficiently manage file writing tasks in their backend projects.

Backend DevelopmentPHPCode Examplefile 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.