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

Unit 11: Directories and Files



            11.2.2 A File or a Directory?                                                         Notes

            You can confirm that the entity you’re testing is a file, as opposed to a directorty, using the
            is_file() function. The is_file () function requires the file path and returns a Boolean value.


            if (is_file(*test.text*)) {
                   Echo *test.txt is file!*;

            }
            Conversely, you might want to check that the entity you’re testing is a directory. You can do this
            with the is_dir() function. is_dir () requires the path to the directory and return a Boolean value.

            if (is_dir(“/tmp”)) {
                   echo  “/tmp is a directory”;

            }
            After you know a file or directory exists, you might need to test its permissions.

            You’ll learn about this in the next section.
            11.2.3 Checking the Status of a File

            When you know that a particular entity exists, and it’s what you expect it to be (either a directory
            or a file), you’ll need to know what you can do with it. Typically, you might want to read, writer
            to, or execute a file. PHP can help you determine whether you can perform these operations.
            The is_readable() function tells you whether you can read a file. On UNIX systems, you might
            be able to see a file but still be barred from reading its contents because of its user permissions.
            The is_readable() function accepts the file path as a string and returns a Boolean value.

            if (is)_readable(“test.txt”)) {
                   echo “test.txt is readable”;

            }
            The is_writable () function tells you whether you have the proper permission to write to a file.
            As with is_readable(), the is_writable () function requires to file path and return a Boolean value.

            if (is_writable (“test.txt”)) {
                   echo “test.txt is writable”;

            }

            The is_executable() function tells you whether you can execute the given file, relying on either
            the file’s permissions or its extension, depending on your platform.

            The function accepts the file path and returns a Boolean value.

            if (is_executable (“test.txt”)) {
                   Echo “test.txt is executable”;

            }


                                             LOVELY PROFESSIONAL UNIVERSITY                                   183
   183   184   185   186   187   188   189   190   191   192   193