A filesystem stores a lot of
additional information about files aside from their actual contents. This
information includes such particulars as the file's size, what directory it's
in, and access permissions for the file. If you're working with files, you may
also need to manipulate this metadata. PHP gives you a variety of functions to
read and manipulate directories, directory entries, and file attributes. Like
other file-related parts of PHP, the functions are similar to the C functions
that accomplish the same tasks, with some simplifications.
Files are organized with inodes . Each file (and other parts of the filesystem, such as
directories, devices, and links) has its own inode. That inode contains a
pointer to where the file's data blocks are as well as all the metadata about
the file. The data blocks for a directory hold the names of the files in that
directory and the inode of each file.
PHP provides two ways to look in a
directory to see what files it holds. The first way is to use opendir(
) to get a directory handle, readdir( ) to iterate
through the files, and closedir( ) to close the directory handle:
$d = opendir('/usr/local/images') or die($php_errormsg);
while (false !== ($f = readdir($d))) {
// process file
}
closedir($d);
The second method is to use the directory class. Instantiate the
class with dir( ), read each filename with the read( ) method,
and close the directory with close( ):
$d = dir('/usr/local/images') or die($php_errormsg);
while (false !== ($f = $d->read())) {
// process file
}
$d->close();
Section 19.8 shows how to use opendir( ) or dir( ) to process each
file in a directory. Making new directories is covered in Section 19.11 and removing directories in Section 19.12.
The filesystem holds more than just files and directories. On
Unix, it can also hold symbolic links. These are special
files whose contents are a pointer to another file. You can delete the link
without affecting the file it points to. To create a symbolic link, use
symlink( ):
symlink('/usr/local/images','/www/docroot/images') or die($php_errormsg);
This creates a symbolic link called images in /www/docroot
that points to /usr/local/images.
To find information about a file, directory, or link you must
examine its inode. The function stat( ) retrieves
the metadata in an inode for you. Section 19.3 discusses stat( ). PHP also has many functions that use
stat( ) internally to give you a specific piece of information about a
file. These are listed in Table
19-1.
|
Function name
|
What file information does the function provide?
|
|---|---|
|
file_exists( )
|
Does the file exist?
|
|
fileatime( )
|
Last access time
|
|
filectime( )
|
Last metadata change time
|
|
filegroup( )
|
Group (numeric)
|
|
fileinode( )
|
Inode number
|
|
filemtime( )
|
Last change time of contents
|
|
fileowner( )
|
Owner (numeric)
|
|
fileperms( )
|
Permissions (decimal, numeric)
|
|
filesize( )
|
Size
|
|
filetype( )
|
Type (fifo, char, dir, block, link, file,
unknown)
|
|
is_dir( )
|
Is it a directory?
|
|
is_executable( )
|
Is it executable?
|
|
is_file( )
|
Is it a regular file?
|
|
is_link( )
|
Is it a symbolic link?
|
|
is_readable( )
|
Is it readable?
|
|
is_writable( )
|
Is it writeable?
|
On Unix, the file permissions indicate what operations the
file's owner, users in the file's group, and all users can perform on the file.
The operations are reading, writing, and executing. For programs, executing
means the ability to run the program; for directories, it's the ability to
search through the directory and see the files in it.
