Updated July 1, 2023
Introduction to PHP Append File
One can append some specific data into a specific file just by using a+ or a mode in the fopen() function. You can perform file append operations in PHP using the fwrite() function. This function allows you to add content to a specific text file. Additionally, you can open the file in append mode using the fopen() function. To write any type of content, one must need to open the file resources/resource in append or write mode only.
Syntax
Syntax and Parameters:
$fp = fopen('movies.txt', 'a')or die("Unable to open file!");
Parameters
Explanation of Parameters of Appending File in PHP:
fopen() Function: The fopen() function of the PHP Programming Language is used to open the text file with different types of modes like reading mode, writing mode, appending mode, etc.. based on the requirement.
Movies.txt: Here in the above syntax, the “movies.txt” parameter is the name with the path of the text file, which is usually used inside the fopen() function. The text file name can be anything.
“a” mode: The “a” mode parameter is an essential parameter. It means the appending mode parameter. It helps in appending some text file data to any file which is specifically mentioned based on our requirement.
die() Function: The die() function will help handle the error only if the file is not available. This is the main purpose of the die() function in the PHP Programming Language.
How does Append File work in PHP?
Appending Files in PHP is a very important concept of PHP Programming in order to handle file data based on our/user requirements. Check out the examples below so that you can understand how the append file is working. Some of the modes of PHP Programming include reading file mode ‘r,’ writing file mode ‘w,’ open file write mode/append-only ‘a’, creating a new file mode for write only ‘x,’ opening a file mode for reading/write ‘r+,’ read and write mode ‘w+,’ another append data mode ‘a+,’ File checking mode ‘x+’ whether it is present or not, etc. Likewise, for different types of data handling works, different types of modes are used. Here ‘a’ mode for appending data is used specifically.
Examples to Implement PHP Append File
Below are the examples mentioned:
Example #1
This is an example of implementing the append file functionality of the PHP Programming Language in order to append some text content to the text file. Here at first, a variable called “fp” is created, which helps in opening the text file “movies.txt,” and then the “fread()” function with echo functionality is used to print the whole content of the text file. Then close() function is used to close the opened text file. The <br> tag is for the line break, and <hr> tag is for the horizontal line is used. Then again, “$fp” is created to open the file “movies.txt” with the help of fopen() function. Then fwrite() function is used to append some data text into the movies.txt file. This fwrite() function is used twice to enter the text content twice to the movies.txt file. Then, at last, echo is used just to show some string output in the browser. Check out the output below in the below section to understand how the append functionality works.
Code:
<?php
$fp = fopen('movies.txt', 'r')or die("Unable to open file!");//opens file in append mode
echo "List of movies in the text file :: <br>";
echo fread($fp,filesize('movies.txt'));
fclose($fp);
echo "<br>";
$fp = fopen('movies.txt', 'a')or die("Unable to open file!");//opens file in append mode
echo "<br>";
echo "Here I am going to add a movie name texts :: 6. The IRon Man, 7. The Hulk";
echo "<br>";
fwrite($fp, '6. The IRon Man ');
echo "<br>";
fwrite($fp, '7. The Hulk');
echo "<br>";
fclose($fp);
echo "<br>";
echo "File appended with the content successfully";
echo "<be>";
?>
Output:
Example #2
This is also a similar example of appending the text into a specific text file ‘persons.txt”. ‘ Here, some html tags are used at first. Those don’t affect anything. Then “$fp1” is used to open the text file, and then fread() function is used to display the whole persons.txt file content in the browser itself just after the PHP file execution. The fclose() function helps in closing the opened file. You can open the text file and encounter the text present in the file.
Code:
<!DOCTYPE html>
<html>
<head>
<title>PHP File Append Concept</title>
</head>
<body>
<?php
$fp1 = fopen('persons.txt', 'r')or die("Unable to open file!");//opens file in append mode
echo "List of persons in the text file :: <br>";
echo fread($fp1,filesize('persons.txt'));
fclose($fp1);
echo "<br>";
echo "<hr>";
$file1 = fopen('persons.txt', 'a');//opens file in append mode
//appending person names
// Appending string text 'Padmavathi'
echo "Now adding string text names Padmavathi and Shekshavali to the text file :: ";
echo "<br>";
echo "<hr>";
fwrite($file1, ' Padmavathi, \n ');
// Appending string text 'Shekshavali'
fwrite($file1, 'Shekshavali');
//Closing the opened file
fclose($file1);
echo "Now the PHP File append concept is done successfully";
?>
</body>
</html>
Output:
Conclusion
I hope you learned what is the definition of a PHP Append File along with its syntax and the explanation of the parameters, How to Append a File in PHP Programming Language, along with various examples of the PHP Append File concept to understand the Append File concept of PHP Programming Language better.
Recommended Articles
This is a guide to PHP Append File. Here we discuss an introduction to PHP Append File, syntax, how it works, and examples with sample code. You can also go through our other related articles to learn more –