How to Write into a File in PHP
In this tutorial, we will learn how to create or write into a file in PHP. Writing a file is simply the process of storing data into a file using PHP functions.
Just like you open a notebook and write down notes, PHP opens a file and writes text, numbers, logs, JSON, or binary data into it.
Common File Writing Functions in PHP
PHP provides multiple functions for writing to files, each suitable for different requirements.
- fwrite()
- file_put_contents()
Let’s understand how each function works to write data into a file with the help of examples.
Writing to a File in PHP with fwrite() Function
The fwrite() function is a built-in PHP function used to write data or content to a file. With this function, you can write small chunks of data, multiple lines, or even structured data step by step into a file.
Basic Syntax
The general syntax looks like this:
fwrite(resource $file, string $string, int $length = ?);Parameters:
The fwrite() function accepts three parameters:
- $file: The file pointer returned by fopen() function.
- $string: The text or data you want to write.
- $length (optional): Maximum number of bytes to write.
Return Value:
- On success, the fwrite() function will return the number of bytes written.
- On failure, this function returns FALSE.
Example 1: Creating and Writing Content to a File
Let’s take a practical example in which we will create a file and write some text into it. But before using fwrite() function, you first need to open the file in a write mode using fopen() function. The most commonly mode to write into a file in PHP is “w”. This mode open a file for write only. So, let’s create a file named writefile.txt and write some text into it.
<?php
// Open file in write mode.
$file = fopen("writingfile.txt", "w");
// Check if the file opened successfully.
if ($file) {
fwrite($file, "This is an example of writing text into a file in PHP.\n");
// Always close the open file.
fclose($file);
echo "Data written successfully!";
} else {
echo "Error: Failed to open the file.";
}
?>Output:
Data written successfully!
Content Written into a File:
This is an example of writing text into a file in PHP.
In this example, the file writingfile.txt is opened in write mode. If it doesn’t exist, PHP creates it. After that, the fwrite() function writes the content into the file, and the fclose() function closes the file. This is the most basic, yet also the most important, example of writing to a file in PHP. Without closing files properly, you risk losing data or leaving files locked unnecessarily.
Example 2: Writing Multiple Lines using fwrite() Function
<?php
// Open file in write mode.
$file = fopen("writingfile.txt", "w");
// Check if the file opened successfully.
if ($file) {
// Writing multiple line using fwrite() function.
fwrite($file, "Line 1: Writing to a file.\n");
fwrite($file, "Line 2: Adding another line.\n");
fwrite($file, "Line 3: PHP is awesome programming language!\n");
// Always close the open file.
fclose($file);
echo "Data written successfully!";
} else {
echo "Error: Failed to open the file.";
}
?>Output:
Data written successfully!
Contents Written to a File:
Line 1: Writing to a file. Line 2: Adding another line. Line 3: PHP is awesome programming language!
When you open the file, you’ll see neatly formatted lines. The newline character \n ensures that each string appears on a new line.
Direct Writing with PHP file_put_contents() Function
The simplest and most convenient way to write small or medium-sized data into a file in PHP is by using the file_put_contents() function. The file_put_contents() function is a built-in PHP function that allows you to write data to a file with just one line of code.
Unlike fopen(), fwrite(), and fclose(), which require multiple lines of code, the file_put_contents() function handles everything automatically—opening, writing, and closing the file.
Basic Syntax
file_put_contents(string $filename, mixed $data, int $flags = 0, resource $context = ?): int|falseParameters:
The file_put_contents() function accepts the following parameters:
- $filename → The name (and path) of the file to write.
- $data → The data to write (string, array, or stream).
- $flags (optional) →
- FILE_APPEND → Appends data to the file instead of overwriting it.
- LOCK_EX → Acquires an exclusive lock while writing, preventing other processes from writing at the same time.
- $context (optional) → A stream context (rarely used).
Return Value:
- On success, the file_put_contents() function returns the number of bytes written.
- On failure, this function returns FALSE.
Example 1: Writing Data into a File
<?php
$data = "This is an example of writing data into a file using file_put_contents() function.";
file_put_contents("writingfile.txt", $data);
echo "Data written successfully!";
?>Output:
Data written successfully!
Contents Written to a File:
This is an example of writing data into a file using file_put_contents() function.
This will create a file named writingfile.txt and write the text data into it. If the file already exists, its content will be overwritten.
Example 2: Appending Data into a File
<?php
$data = "Appending a new data at the end!" . "\n";
file_put_contents("writingfile.txt", $data, FILE_APPEND);
echo "New data added!";
?>Output:
New data added!
Contents Written to a File:
This is an example of writing data into a file using file_put_contents() function. Appending a new data at the end!
In this example, instead of overwriting the file, we used FILE_APPEND flag as the third parameter to add new data content at the end of the file.
Conclusion:
Writing to files is a fundamental task in PHP. You can achieve it using functions like fwrite() and file_put_contents(). Both functions are useful, but they serve different purposes. The fwrite() function provides more control for complex or large file operations, while file_put_contents() function is simpler and best for quick file writing.
With these functions, you can write not only plain text but also binary data such as images, PDFs, or serialized objects, which makes PHP file handling both powerful and flexible.



