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. The PHP Append to file is done just by using the fwrite() function of PHP Programming Language. The PHP Append functionality can also be opened with the help of the file open function “fopen()” with the help of an append mode. In order to append some content, a specific text file is needed to add some more data or it. To write any type of content one must need to open the file resources/resource in append or write mode only.
Syntax and Parameters:
$fp = fopen('movies.txt', 'a')or die("Unable to open file!");
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 in handling the error only if the file is not available like that. This is the main purpose of the die() function in the PHP Programming Language.
How does Append File work in PHP?
Appending File in PHP is a very important concept of PHP Programming in order to handle file data based on our/user requirement. In order to append file text/file to the file, a mode or a+ mode is needed to be mentioned in the fopen() function to handle the file data. The “read()” function is used to read the data which is present in the text file. The “fwrite()” function of the PHP Programming Language is used to write the data to the text file/other. Check out the examples below so that you can understand how the append file is working.
Some of the modes of the PHP Programming includes reading file mode ‘r’, writing file mode ‘w’, open file write mode/append-only ‘a’, creating 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 a different type 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:
This is the example of implementing the append file functionality of 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 “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 line break and <hr> tag is for 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.
$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>";
$fp = fopen('movies.txt', 'a')or die("Unable to open file!");//opens file in append mode
echo "Here I am going to add a movie name texts :: 6. The IRon Man, 7. The Hulk";
fwrite($fp, '6. The IRon Man ');
fwrite($fp, '7. The Hulk');
echo "File appended with the content successfully";
This is also a similar example of appending the text into a specific text file ‘persons.txt”. Here at first some html tags are used. 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. Likewise, again fopen() function is used but this time append mode ‘a’ is used and this helps in getting the functionality of appending the content. Then fwrite() function is used in order to add the content to the specific text file ‘persons.txt’ but the path should be correct if the file present somewhere rather than present in the working folder. Here also fwrite() function is used twice to enter some text content twice into the persons.txt file. You can open the text file and encounter the text present in the file.
<title>PHP File Append Concept</title>
$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>";
$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 :: ";
fwrite($file1, ' Padmavathi, \n ');
// Appending string text 'Shekshavali'
//Closing the opened file
echo "Now the PHP File append concept is done successfully";
I hope you learned what is the definition of PHP Append File along with its syntax and the explanation of the parameters, How to Append File in PHP Programming Language along with various examples of PHP Append File concept to understand the Append File concept of PHP Programming Language better.
This is a guide to PHP Append File. Here we discuss an introduction to PHP Append File, syntax, how does it work, examples with sample code. You can also go through our other related articles to learn more –