PHP programming language provides different functions in order to write files. A file is a container that contains and stores data in different formats like simple ASCII tet, binary data, XML data, etc. Even databases provide more structured and easy to use data storage functionality files can provide more simple, fast, and basic data storage functionality.
Create A File
Even this tutorial is about writing a file in some cases we need to create a new file in order to write. So first we will create a file in order to write and put data into it later. We will use the fopen() function in order to create a file. fopen() function has the following syntax.
fopen(string $filename , string $mode , bool $use_include_path , resource $context)
- $filename is a must where the file name or the complete path with filename is provided.
- $mode is a must where the file opening mode is specified. In order to create new file w and w+ modes can be used. But if the files allready exist and contains data only the w+ should be used for write operation.
- $use_include_path and $content are optional parameters and doesn’t need for a write operation in general.
In the following example we will open the a file named myfile.txt with write mode.
<?php
//Open file and create if doesn't exist and overwrite if exist
$myfile = fopen("myfile.txt","w");
//Open file and create if doesn't exist
and do not overwrite if exist
$myfile = fopen("myfile.txt","w+");
?>
Be we will list file opening modes.
Symbol | Mode |
r | Open file read-only |
r+ | Open file reading and writing and place cursor at the begging |
w | Open file for writing only, remove existing data |
w+ | Open file for reading and writing, remove existing data |
a | Open file for writing only put the cursor at the end and if the file doesn’t exist create |
a+ | Open file for reading and writing, place cursor at the end of the file, create the file if it doesn’t exist. |

Write File with fwrite() Function
fwrite() is the most popular function used to write a file. Before using the fwrite() function the file should be opened with the fopen() function properly. fwrite() function writes given string value which is a simple text into the specified and opened file. It has the following syntax.
fwrite ( resource $handle , string $string , int $length )
- $handle is the file handle or resource which is opened with the fopen() function. This parameter is a must.
- $string is the text value we want to write into the given file. This parameter is a must.
- $length parameter is used to specify how many characters of the given $string should be written to the file. This parameter is optional and if not provided all data in the string will be written into the file.
In the following example, we will write the “I am wisetut.com” string into the file named myfile.txt. We will do not provide any length information which means all provided string will be written into the myfile.txt.
<?php
//Open file and create if doesn't exist and overwrite if exist
$myfile = fopen("myfile.txt","w");
//Create text we want to write
$txt = "I am wisetut.com";
//Write given text into the file
$fwrite($myfile,$txt);
//Close file to write changes to the disk permanently
$fclose($myfile);
?>
Writing A File By Adding After Existing Content
If we want to write a file with an existing content and do not want to overwrite and delete all previous content we should use the w+ mode while opening file. In this example we will add text into the existing data.
<?php
//Open existing file to add data.
$myfile = fopen("myfile.txt","w+");
//Create text we want to write
$txt = "I am wisetut.com";
//Add given text into the file
$fwrite($myfile,$txt);
//Close file to write changes to the disk permanently
$fclose($myfile);
?>
Write File with file_put_contents() Function
Another function that can be used to write into a file is file_put_contents() function. file_put_contents() function provides more practical way to write a file according to fwrite()function. It has the following syntax where you do no t need to open or close a file. So only calling this function will put given text into the given file where file opening and closing will be done automatically during file_put_contents() function.
file_put_contents ( string $filename , mixed $data , int $flags = 0 , resource $context )
- $filename is the file name and path we want to write into. This parameter is a must.
- $data is the data we want to put into the specified file. This parameter is a must and can be in different data types like string, array, etc. But in real-world applications mostly string data is provided.
- $flags parameter is used to provide some file write options like
FILE_USE_INCLUDE_PATH
,FILE_APPEND
,LOCK_EX
. This parameter is optional.
In the following example we will write “I am wisetut.com” into the file named myfile.txt.
<?php
file_put_contents("myfiles.txt","I am wisetut.com");
?>
If you call the file_put_contents() function again an existing file that has some data all existing data will be erased by default. Hopefully, the FILE_APPEND option can be used to add provided text into the existing text in the file. In the following example, we will add the text “I am poftut.com” after the existing text in myfile.
<?php
file_put_contents("myfiles.txt","I am Wisetut.com",FILE_APPEND);
?>
fwrite() vs file_put_contents()
Even the file_put_contents has very practical and sage usage it has some disadvantages. Every time the file_put_contents() function is called the given file will be opened provided string will be written and the file will be closed. This will create overhead for multiple write operations. If you need to write multiple lines faster you should use the fwrite() function. According to the different benchmarks, the fwrite() function is 3 times faster than file_put_contents() function.
How To Write A Text File?
One of the most popular PHP write file example is writing into a text file. There are two methods to write into a text file. First, we can open a file with fopen() method and write fwrite() method and close with the fclose() method like below.
<?php
//Open existing file to add data.
$myfile = fopen("textfile.txt","w+");
//Create text we want to write
$txt = "I am wisetut.com";
//Add given text into the file
$fwrite($textfile,$txt);
//Close file to write changes to the disk permanently
$fclose($textfile);
?>
Or more practical way is using the file_put_contents() method where this single method can write into given text file.
<?php
//Write into text file and overwrite existing content
file_put_contents("textfile.txt","I am Wisetut.com");
//Write into a text file and append existing content
file_put_contents("textfile.txt","I am Wisetut.com",FILE_APPEND);
?>
How To Write Log File In PHP?
Logs are used to store important events about the operating system, application, services, users, etc. Log files are generally stored as a simple text file in a structured manner. We can use the PHP language in order to write into a log file that can be useful in web applications. Log files are generally stored in /var/log/ or /var/www/app1 etc. We append a new log files with some delimiters like ; or | or : etc.
<?php
//Write into a log file name logs.txt
$log = "User ali:date 12-12-2020:action remove:resource /test";
file_put_contents("/var/www/app1/logs.txt",$log,FILE_APPEND);
?>