Updated April 6, 2023
Introduction to PHP Write File
There are various in-built functions in PHP which are used to perform various actions upon a file. They maybe to create, open, read, write and other operations on the file.
Functions of PHP Write File
Below are the major functions available by default in the PHP below:
First, in order to write to a file, we must know how to create that file. This is done with the help of the open() function. The name might be misleading as to open a file, but in PHP, the same function is used for creating and opening the file, just like vi functions in Linux. This function, as soon as it is executed, checks for the file if it exists and then only creates it. The example below demonstrates the same:
<?php // Creating a file for test $myfile = fopen("test.txt", "w") ?>
After creating a file, we have to write the required contents into it, and hence we use this function for the same. This function will stop only when it reaches the end of the file (EOF) or the length we specify according to the order which comes first.
fwrite(file, string, length)
- where the file is the mandatory field which describes the file we should write to
- the string is another required parameter which tells the string to write to the opened file
- length is an optional parameter and gives us the maximum number of bytes to be written
<?php // Your code here! $f = fopen("testfile.txt", "w"); $text = "Random data here\n"; fwrite($f, $text); ?>
Here testfile.txt is the file created, and the string value assigned to $text will be written to that file.
This is another function which can be used to write contents to a file in PHP. There are a certain number of rules in the same order mentioned to be followed when accessing a file:
- There is a property called FILE_USE_INCLUDE_PATH, and it checks the path if it includes a copy of the filename when it is set.
- Creates a file after checking whether it exists or not
- Next, it opens the file
- If the property LOCK_EX is set, then it locks the file
- When the property FILE_APPEND is set, it moves to the end of the file else clears the contents of the file.
- Now it writes the required data to the file.
- Closes the file and release if any locks are present
file_put_contents(filename, text, mode, context)
- where filename is the mandatory parameter and tells us the entire path of the file where we have to write to, this function hence checks and creates a file.
- text is another mandatory field which is the data we have to write to our file. It can be in the form of a simple string, an array of strings or a data stream.
- mode is the optional field which gives us various ways to operate on the file. Its possible values are:
- FILE_USE_INCLUDE_PATH: This searches for specified filename in the include directory path.
- FILE_APPEND: It appends the data to the file instead of overwriting the same.
- LOCK_EX: This puts an explicit lock on the file when being written.
- context is the optional parameter which gives the context of the file. It is basically a bunch of options which can alter the stream’s behavior.
Return Values: This function returns the total number of bytes which are written to the file in case of a SUCCESS and returns value FALSE if failed.
Below is the example:
<?php echo file_put_contents("filetest.txt","Testing for file write"); ?>
Here the file we are creating is given as the first parameter, and the next parameter is the text written into that file.
We can overwrite to the above file in which data has been already written. Whatever data is already present in the file is cleaned off, and it will start as a brand new empty file. In the below examples, we will open an existing file and try writing new data on it.
Below is the example:
<?php $f = fopen("testfile.txt", "w"); $text = "Random data here\n"; $filetext = "Over writing the data\n"; fwrite($f, $filetext); $filetext = "File Dummy Data\n"; fwrite($f, $filetext); fclose($f); ?>
Here we are overwriting data in testfile.txt, so whatever is mentioned in the $filetext string value, the same will be written to the file. And when we use the same write command again by changing data given to the $filetext, then the old data is cleaned up and occupied by the latest text value that is provided.
At the end of the file, we always should close it by using the close() function which we have opened using the fwrite() function. As shown in the above example, we also use the \n, which represents the newline equivalent to pressing the enter button on our keyboard.
Now let us take an example and see how to include more data in our file.
Examples to Implement of PHP Write File
Below are the examples of PHP Write File:
First, we add 2 lines of data using the below code:
<?php $testf = "TestFile.txt"; $filehandle = fopen($testf, 'w'); $fdata = "Julian Caesar\n"; fwrite($filehandle, $fdata); $fdata = "Harry James\n"; fwrite($filehandle, $fdata); print "Data writing completed"; fclose($filehandle); ?>
This creates a file TestFile.txt having 2 lines of data as mentioned.
We will append another 2 names in the same file as shown below:
<?php $testf = "TestFile.txt"; $filehandle = fopen($testf, 'a'); $fdata = "Lilly Potter\n"; fwrite($filehandle, $fdata); $fdata = "Edward Cullen\n"; fwrite($filehandle, $fdata); print "Data has been appended"; fclose($filehandle); ?>
This example appends the given names to the same file as in the first example.
As shown above, there are various methods and steps that need to be followed when we want to write to a file in PHP. fwrite() is one of the main functions to do this and is used majorly for writing data to a file. They can do basic writing of data to our file but should be used in combination with other mandatory functions like open() and close(), without which it is not possible to perform operations on the file if it does not exist.
This is a guide to the PHP Write File. Here we discuss the Introduction and its Functions of PHP Write File along with examples and Code Implementation. You can also go through our other suggested articles to learn more-