Organizing related data into entities commonly referred to as files and directories has long been a core concept in the computing environment. For this reason, programmers need to have a means for obtaining important details about files and directories, such as the location, size, last modification time, last access time, and other defining information. This section introduces many of PHP's built-in functions for obtaining these important details. Parsing Directory Paths It's often useful to parse directory paths for various attributes, such as the tailing extension name, directory component, and base name. Several functions are available for performing such tasks, all of which are introduced in this section. basename() string basename (string path [, string suffix]) The basename() function returns the filename component of path. If the optional suffix parameter is supplied, that suffix will be omitted if the returned file name contains that extension. An example follows: <?php dirname() string dirname (string path) The dirname() function is essentially the counterpart to basename(), providing the directory component of path. Reconsidering the previous example: <?php $path = "/home/www/data/users.txt"; pathinfo() array pathinfo (string path) The pathinfo() function creates an associative array containing three components of the path specified by path: directory name, base name, and extension, referred to by the array keys dirname, basename, and extension, respectively. Consider the following path: /home/www/htdocs/book/chapter10/index.html As is relevant to pathinfo(), this path contains three components:
Therefore, you can use pathinfo() like this to retrieve this information: <?php This returns: --------------------------------------------Dir name: /home/www/htdocs/book/chapter10 Base name: index.html realpath() string realpath (string path) The useful realpath() function converts all symbolic links, and relative path references located in path, to their absolute counterparts. For example, suppose your directory structure assumed the following path: /home/www/htdocs/book/images/ You can use realpath() to resolve any local path references: <?php Please check back for the second part of this series.
blog comments powered by Disqus |
|
|
|
|
|
|
|