Saturday, September 30, 2017

PHP Append to File

skptricks PHP Append to File

PHP allows to append data into file by using a or a+ mode in fopen() function.

File "file.txt" consists of below data initially, lets see the content of file below :
welcome to skptricks

PHP Append to File - fwrite() :
The PHP fwrite() function is used to write and append data into file.

Example :
<?php  
//open file in append mode  
$fp = fopen('data.txt', 'a');
//writing data to file
fwrite($fp, ' today is great day ');  
fwrite($fp, 'but it is raining');  
//closing file
fclose($fp);  
echo "File appended successfully";  
?>
Output:
----------------
welcome to skptricks today is great day but it is raining

NOTE : make sure while appending the data to file, you must open file in append mode. Otherwise it will erase the previous content/data of file.




No comments:

Post a Comment