14 – PHP File Handling: File Append

0
453

In our previous tutorial we have learned how to open, close, read and write to a file. However, the ways which we wrote to a file so far have caused data that was stored in the file to be deleted. So in this lesson we’re going to deal on how to append to a file or to add on to the existing data, but we need to open the file in append mode.
 

To start with, this application and if we want to add on to a file we need to open it up in append mode. To do this,  let’s create first a new file PHP file called “append.php”. And add the following code:

Take note, that when we were to write to a file it would begin writing data at the of the file.

  1. <?php
  2.  
  3. //specify the filename and we call it as hello.txt
  4. $testfile = 'hello.txt';
  5. //and inside the if statement, we pass the $testfile variable to fopen and we specify the mode as write
  6. //and fopen return a handle as a value, and using handle is useful because we could close it
  7. if($fh = fopen($testfile, 'a')){// we are in append mode
  8. }else{
  9. //if the file can't be open..and it can prevent some error might happen
  10. echo 'Could not open the file';
  11. }
  12. ?>

If you open the hello.txt file, you can see that it will look like as shown below.

 

hello
World

 

For appending data, we’re going to use the hello.txt file that we have created in our previous tutorial called “Writing to Files”. At this time we will modify our code above to do the appending of data. And here’s the following code.

  1. <?php
  2.  
  3. //specify the filename and we call it as hello.txt
  4. $testfile = 'hello.txt';
  5. //and inside the if statement, we pass the $testfile variable to fopen and we specify the mode as write
  6. //and fopen return a handle as a value, and using handle is useful because we could close it
  7. if($fh = fopen($testfile, 'a')){// we are in append mode
  8. $somestring = "\nnew append data 1\n";
  9. fwrite($fh, $somestring);
  10. $somestring = "new append data 2\n";
  11. fwrite($fh, $somestring);
  12. //close the handle
  13. fclose($fh);
  14. }else{
  15. //if the file can't be open..and it can prevent some error might happen
  16. echo 'Could not open the file';
  17. }
  18.  
  19. ?>

 

 
You should noticed that the way we write data to the file is exactly the same as in the write lesson. The only thing that is different is that the file pointer is placed at the end of the file in append mode, so all data is added to the end of the file.

 

After executing the code above, you can notice that the way we write data to the file is exactly the same as in the Writing to File lesson.  But the only difference is that the pointer is placed at the end of the file in append mode so all data is added to the end of the file. And the hello.txt file will look like as shown below.

 
hello
World
new append data 1
new append data 2

 

Facebook Comments
(Visited 25 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here