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

PHP Tizag Tutorial-61 PDF

Opening an existing file for writing in PHP wipes the file clean and starts with an empty file. The code example overwrites the contents of the testFile.txt file by opening it in write mode, writing the strings "Floppy Jalopy" and "Pointy Pinto" line by line, and closing the file. When reopened, the original contents of Bobby and Tracy are gone, replaced by the newly written data as expected when opening a file in write mode.

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)
60 views

PHP Tizag Tutorial-61 PDF

Opening an existing file for writing in PHP wipes the file clean and starts with an empty file. The code example overwrites the contents of the testFile.txt file by opening it in write mode, writing the strings "Floppy Jalopy" and "Pointy Pinto" line by line, and closing the file. When reopened, the original contents of Bobby and Tracy are gone, replaced by the newly written data as expected when opening a file in write mode.

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 Write: Overwriting

Now that testFile.txt contains some data we can demonstrate what happens when you open an existing
file for writing. All the data contained in the file is wiped clean and you start with an empty file. In this example
we open our existing file testFile.txt and write some new data into it.

PHP Code:
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);

If you now open the testFile.txt file you will see that Bobby and Tracy have both vanished, as we
expected, and only the data we just wrote is present.

Contents of the testFile.txt File:


Floppy Jalopy
Pointy Pinto

In the next lesson we will show you how to get information out of a file by using PHP's read data functions!

You might also like