File Types and Links
Numerous functions are available for learning various details about files and links (or file pointers) found on a file system. Those functions are introduced in this section.
string filetype (string filename)
The filetype() function determines and returns the file type of filename. Eight values are possible:
Let's consider three examples. In the first example, you determine the type of a CD-ROM drive:
echo filetype("/mnt/cdrom"); // char
Next, you determine the type of a Linux partition:
echo filetype("/dev/sda6"); // block
Finally, you determine the type of a regular old HTML file:
echo filetype("/home/www/htdocs/index.html"); // file
int link (string target, string link)
The link() function creates a hard link, link, to target, returning TRUE on success and FALSE otherwise. Note that because PHP scripts typically execute under the guise of the server daemon process owner, this function will fail unless that user has write permissions within the directory in which link is to reside.
int linkinfo (string path)
The lstat() function is used to return useful information about a symbolic link, including items such as the size, time of last modification, and the owner's user ID. The linkinfo() function returns one particular item offered by the lstat() function, used to determine whether the symbolic link specified by path really exists. This function isn't available for the Windows platform.
array lstat (string symlink)
The lstat() function returns numerous items of useful information regarding the symbolic link referenced by symlink. See the following section on fstat() for a complete accounting of the returned array.
array fstat (resource filepointer)
The fstat() function retrieves an array of useful information pertinent to a file referenced by a file pointer, filepointer. This array can be accessed either numerically or via associative indices, each of which is listed in its numerically indexed position:
Consider the example shown in Listing 10-1.
Listing 10-1. Retrieving Key File Information
/* Convert timestamp to desired format. */
$file = "/usr/local/apache2/htdocs/book/chapter10/stat.php";
/* Retrieve file information */
/* Output some juicy information about the file. */
This code returns:
array stat (string filename)
The stat() function returns an array of useful information about the file specified by filename, or FALSE if it fails. This function operates exactly like fstat(), returning all of the same array elements; the only difference is that stat() requires an actual file name and path rather than a resource handle.
If filename is a symbolic link, then the information will be pertinent to the file the symbolic link points to, and not the symbolic link itself. To retrieve information about a symbolic link, use lstat(), introduced a bit earlier in this chapter.
string readlink (string path)
The readlink() function returns the target of the symbolic link specified by path, or FALSE if an error occurs. Therefore, if link test-link.txt is a symbolic link pointing to test.txt, the following will return the absolute pathname to the file:
int symlink (string target, string link)
The symlink() function creates a symbolic link named link to the existing target, returning TRUE on success and FALSE otherwise. Note that because PHP scripts typically execute under the guise of the server daemon process owner, this function will fail unless that daemon owner has write permissions within the directory in which link is to reside. Consider this example, in which symbolic link "03" is pointed to the directory "2003":
blog comments powered by Disqus