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

Unit 11: Directories and Files



               •  Understand the creating file.                                                   Notes
               •  Discuss the deleting file.

               •  Explain the opening a file for reading.
               •  Discuss the writing files.

               •  Understand the appending files.

            Introduction

            The presentation of the file lessons will begin with how to create, open, and close a file. After
            establishing those basics, we will then cover other important file tasks, such as: create, open,
            read, write and append, files with PHP.
            Linux stores data and programs in files. These are organized in directories. In a simple way,
            a directory is just a file that contains other files (or directories).
            The part of the hard disk where you are authorized to save data is called your home directory.
            Normally all the data you want will be saved in files and directories in your home directory.
            To find your home directory (if you need), type:
            echo $HOME

            The symbol ~ can also be used for your home directory.
            There is a general directory called/tmp where every user can write files. But files in/tmp usually
            get removed (erased) when the system boots or periodically, so you should not store in/tmp
            data that you want to keep permanently.

            A file can be fully and uniquely identified by its full name, including all directories to which it
            belongs. The system starts at the root directory, with name/it “splits” into (sub) directories, and
            these split further, and so on, until you get to a file. For example, a home directory could be/
            usr15/pablo, on which there is a directory called programming, with a directory inside called
            include, on which there is a file called time.h, the full path of this last file will be/usr15/pablo/
            programming/include/time.h

            11.1 Including Files with PHP Include( ) Function


            The include() function enables you to incorporate file into another php file and the included file
            will run as it’s part of that file. For example, if you want display standard or same information
            more than one page. It might be useful to create separate file and incorporate with any other
            file. It makes easier to maintain the information by changing one file instead of going through
            all the files.

            Here is how you would use php include function.

            <?php include(“topMenu.php”) ?>
            or you can include it this way if the file is located in different directory

            <?php include(“/root/topMenu.php”); ?>
            Following code is an example of simple php code utilizing include() function

            <html>


                                             LOVELY PROFESSIONAL UNIVERSITY                                   181
   181   182   183   184   185   186   187   188   189   190   191