0% found this document useful (0 votes)
48 views

PHP Tizag Tutorial-65

The document discusses appending to files in PHP. It explains that opening a file in append mode using the 'a' flag allows adding to the existing data in a file rather than overwriting it. It provides code samples of opening a file in append mode and writing additional string data to the end of the file, preserving any existing contents.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

PHP Tizag Tutorial-65

The document discusses appending to files in PHP. It explains that opening a file in append mode using the 'a' flag allows adding to the existing data in a file rather than overwriting it. It provides code samples of opening a file in append mode and writing additional string data to the end of the file, preserving any existing contents.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP - File Append

So far we have learned how to open, close, read, and write to a file. However, the ways in which we have
written to a file so far have caused the data that was stored in the file to be deleted. If you want to append to a
file, that is, add on to the existing data, then you need to open the file in append mode.

PHP - File Open: Append

If we want to add on to a file we need to open it up in append mode. The code below does just that.

PHP Code:
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a');

If we were to write to the file it would begin writing data at the end of the file.

PHP - File Write: Appending Data

Using the testFile.txt file we created in the File Write lesson , we are going to append on some more data.

PHP Code:
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "New Stuff 1\n";
fwrite($fh, $stringData);
$stringData = "New Stuff 2\n";
fwrite($fh, $stringData);
fclose($fh);

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.
The contents of the file testFile.txt would now look like this:

Contents of the testFile.txt File:


Floppy Jalopy
Pointy Pinto
New Stuff 1
New Stuff 2

You might also like