Master PHP fwrite(): Write Strings, Arrays, and More to Files

This guide explains the PHP fwrite() function, covering its syntax, parameters, and practical examples for writing strings, serialized arrays, and handling file opening modes, helping developers reliably write data to files in backend applications.

php Courses
php Courses
php Courses
Master PHP fwrite(): Write Strings, Arrays, and More to Files

The fwrite() function in PHP provides a simple and flexible way to write data to files, a common requirement in backend development.

Syntax

fwrite(file, string, length)

Parameters

file

: Required. The file handle returned by fopen(). string: Required. The data to write; can be a string, array (after serialization), or other types. length: Optional. Maximum number of bytes to write. Defaults to the length of the data.

Basic Usage Example

The following example demonstrates opening a file, writing a simple string, and closing the file.

<?php
$file = fopen("test.txt", "w"); // open for writing
if ($file) {
    $content = "Hello, World!"; // content to write
    fwrite($file, $content); // write to file
    fclose($file); // close file
    echo "Write successful!";
} else {
    echo "Unable to open file!";
}
?>

This script uses fopen() to open test.txt in write mode, writes the string "Hello, World!" with fwrite(), then closes the handle with fclose(). It outputs a success or failure message based on the operation result.

Writing an Array (Serialized)

Beyond plain strings, fwrite() can write serialized data. The example below writes an array of names to a file.

<?php
$file = fopen("data.txt", "w");
if ($file) {
    $data = array("张三", "李四", "王五");
    fwrite($file, serialize($data)); // write serialized array
    fclose($file);
    echo "Write successful!";
} else {
    echo "Unable to open file!";
}
?>

First, an array $data is defined, then serialize() converts it to a storable string, which fwrite() writes to data.txt. After closing the file, a success message is displayed.

Key Points

Always open the file with the appropriate mode (e.g., "w" for writing).

Check that the file handle is valid before attempting to write.

When writing non‑string data, serialize it first to ensure correct storage.

Close the file with fclose() to release resources.

Using fwrite() correctly enables reliable file output for a variety of backend tasks.

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.

file-handlingfwritewrite to file
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.