Page 195 - Open Soource Technologies 304.indd
P. 195

Open Source Technologies



                   Notes         But wait! Take a look at that ‘\n’ newline character. That adds a line break in the file. If you
                                 hadn’t put that in, the file would just contain.
                                 This has been written to the file. This has been appended to the file.

                                 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. 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);

                                 The contents of the file testFile.txt would now look like this:

                                                Floppy Jalopy

                                                Pointy Pinto
                                                New Stuff 1

                                                New Stuff 2

                                 The above example may not seem very useful, but appending data onto a file is actually used
                                 every day. Almost all web servers have a log of some sort. These various logs keep track of all
                                 kinds of information, such as: errors, visitors, and even files that are installed on the machine.
                                 A log is basically used to document events that occur over a period of time, rather than all at
                                 once. Logs: a perfect use for append!

                                 11.7.1 File Read

                                 In this lesson, we will teach you how to read data from a file using various PHP functions.
                                 11.7.2 File Open: Read

                                 Before we can read information from a file we have to use the function fopen to open the file
                                 for reading. Here’s the code to read-open the file we created in the PHP File Write lessons.



        190                               LOVELY PROFESSIONAL UNIVERSITY
   190   191   192   193   194   195   196   197   198   199   200