Page 337 - Beginning PHP 5.3
P. 337

Chapter 11: Working with Files and Directories
                           Time - Related Properties

                           Besides their contents, files have other properties that can provide useful information. The available
                         properties depend on the operating system in which the files are created and modified. On UNIX
                         platforms such as Linux, for example, properties include the time the file was last modified, the time it
                         was last accessed, and the user permissions that have been set on the file.

                           PHP provides three time - related file functions:
                            ❑       fileatime()   —   Returns the time at which the file was last accessed as a UNIX timestamp.

                                A file is considered accessed if its contents are read

                            ❑       filectime()  —  Returns the time at which the file was last changed as a UNIX timestamp.
                                A file is considered changed if it is created or written, or when its permissions have been
                                changed

                            ❑       filemtime()  —  Returns the time at which the file was last modified as a UNIX timestamp.
                                The file is considered modified if it is created or has its contents changed
                               A UNIX timestamp is an integer value indicating the number of seconds between the UNIX epoch
                             (midnight on January 1, 1970) and the specified time and date.

                           The   getdate()  function is very useful when working with UNIX timestamps. It returns an associative
                         array containing the date information present in a timestamp. The array includes such values as the year,
                         the month, the day of the month, and so on. For example, you can set a variable such as   $myDate  to the
                          value returned by   getdate() , and then access the month component with  $myDate[ “ month “  .
                                                                                                     ]
                               Find out more about working with dates and times in Chapter 16.

                           Retrieving a Filename from a Path

                           It ’ s often very useful to be able to separate a filename from its directory path, and the  basename()
                          function does exactly that, taking a complete file path and returning just the filename. For example, the
                          following code assigns   index.html  to  $filename :


                             $filename = basename( “/home/james/docs/index.html” );
                           You can specify a directory path instead, in which case the rightmost directory name is returned. Here ’ s
                         an example that assigns the value   docs  to  $dir :

                             $dir = basename( “/home/james/docs” );
                          Basically,  basename()  retrieves the last whole string after the rightmost slash.

                           If you don ’ t want the filename extension, or suffix, you can strip that off too by supplying the suffix as a
                         second argument to   basename() . The following example assigns   “ myfile ”   to  $filename :



                             $filename = basename( “/home/james/docs/myfile.doc”, “.doc” );



                                                                                                         299





                                                                                                      9/21/09   9:10:11 AM
          c11.indd   299
          c11.indd   299                                                                              9/21/09   9:10:11 AM
   332   333   334   335   336   337   338   339   340   341   342