Page 352 - Beginning PHP 5.3
P. 352

Part III: Using PHP in Practice

                                   Digit Value      Permission
                                     3             Can write to and execute the file
                                   4             Can only read the file

                                   5             Can read and execute the file
                                     6             Can read and write to the file

                                     7             Can read, write to, and execute the file


                  Here are some commonly used examples to make the concept of file modes clearer:
                    // Owner can read and write the file; everyone else can just read it:
                    chmod( “myfile.txt”, 0644 );

                    // Everyone can read and write the file:
                    chmod( “myfile.txt”, 0666 );

                    // Everyone can read and execute the file, but only the owner can write to it:
                    chmod( “myfile.txt”, 0755 );

                    // Only the owner can access the file, and they can only read and write to it:





                    chmod( “myfile.txt”, 0600 );
                      Note that you can only change the permissions of a file or directory if you own it, or if you ’ re the
                    super - user (which is highly unlikely for PHP scripts running on a Web server).
                   So how do modes work with directories? Well, to read the files in a directory, you need to have both read
                 and execute permissions on that directory. Meanwhile, to create and delete files and subdirectories inside
                 the directory, you need to have write and execute permissions on the directory.

                  Checking File Permissions
                   Before you do something to a file in your script, it can be useful to know what kinds of things your script
                 can do with the file. PHP provides three handy functions to help you out.
                   To check if you ’ re allowed to read a file, use   is_readable() , passing in the filename of the file to check.
                Similarly, you can check that you ’ re allowed to write to a file with   is_writable() , and see if you can
                 execute a file with   is_executable() . Each function returns  true  if the operation is allowed, or  false
                 if it ’ s disallowed. For example:
                    if ( is_readable( “myfile.txt” ) {
                      echo “I can read myfile.txt”;
                    }

                    if ( is_writable( “myfile.txt” ) {






              314





                                                                                                      9/21/09   9:10:16 AM
          c11.indd   314                                                                              9/21/09   9:10:16 AM
          c11.indd   314
   347   348   349   350   351   352   353   354   355   356   357