Page 292 - Computer Software Application TP - Volume 1
P. 292

COMPUTER SOFTWARE APPLICATION - CITS




                                              //  Specify the file name
                         $fileName = ‘example.txt’;
                         // Open the file in write mode (‘w’)

                         $fileHandle = fopen($fileName, ‘w’);


                         //  Check if the file is opened successfully
                         if ($fileHandle)

                         {
                                 //  Write content to the file
                                 fwrite($fileHandle, ‘This is a line of text.’);


                                 // Close the file handle
                                 fclose($fileHandle);



                                 echo “File ‘$fileName’ has been written successfully.”;
                         }
                         else

                         {
                                 // Display an error message if the file cannot be opened
                                 echo “Error: Unable to open the file for writing.”;
                         }
              ?>

                     </body>
                             </html>
           3  Save the program in C:\Apache24\htdocs in a folder with .php extension
           4  Run the Apache services from windows services
           5  Open the browser and type the following address

                                  http://localhost/foldername/
           6  Click the php file to run  and verify the output













           TASK 7: Opening a File for appending content
           1  Open the text editor








                                                           277
 CITS : IT & ITES - Computer Software Application - Exercise 55  CITS : IT & ITES - Computer Software Application - Exercise 55
   287   288   289   290   291   292   293   294   295   296   297