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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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 handle the error only if the file is not available. This is the main purpose of the die() function in the PHP Programming Language.
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.
Below are the examples mentioned:
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
tag is for the line break, and
Code:
"; echo fread($fp,filesize('movies.txt')); fclose($fp); echo "
"; $fp = fopen('movies.txt', 'a')or die("Unable to open file!");//opens file in append mode echo "
"; echo "Here I am going to add a movie name texts :: 6. The IRon Man, 7. The Hulk"; echo "
"; fwrite($fp, '6. The IRon Man '); echo "
"; fwrite($fp, '7. The Hulk'); echo "
"; fclose($fp); echo "
"; echo "File appended with the content successfully"; echo ""; ?>
Output:
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:
PHP File Append Concept "; echo fread($fp1,filesize('persons.txt')); fclose($fp1); echo "
"; echo "
"; $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 "
"; echo "
"; 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"; ?>
Output:
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.
The above is the detailed content of PHP Append File. For more information, please follow other related articles on the PHP Chinese website!