Specifications
The fileperms() function returns the permissions on the file. We have reformatted them as an
octal number using the decoct() function to put them into a format more familiar to UNIX
users.
The filetype() function returns some information about the type of file being examined. The
possible results are fifo, char, dir, block, link, file, and unknown.
The filesize() function returns the size of the file in bytes.
The second set of functions—is_dir(), is_executable(), is_file(), is_link(), is_
readable(), and is_writable()—all test the named attribute of a file and return true or
false.
We could alternatively have used the function stat() to gather a lot of the same information.
When passed a file, this returns an array containing similar data to these functions. The
lstat() function is similar, but for use with symbolic links.
All the file status functions are quite expensive to run in terms of time. Their results are therefore
cached. If you want to check some file information before and after a change, you need to call
clearstatcache();
in order to clear the previous results. If you wanted to use the previous script before and after
changing some of the file data, you should begin by calling this function to make sure the data
produced is up-to-date.
Changing File Properties
In addition to viewing file properties, we can alter them.
Each of the chgrp(file, group), chmod(file, permissions), and chown(file, user)
functions behaves similarly to its UNIX equivalent. None of these will work in Windows-based
systems, although
chown() will execute and always return true.
The
chgrp() function is used to change the group of a file. It can only be used to change the
group to groups of which the user is a member unless the user is root.
The chmod() function is used to change the permissions on a file. The permissions you pass to
it are in the usual UNIX chmod form—you should prefix them with a “0” to show that they are
in octal, for example,
chmod(“somefile.txt”, 0777);
The chown() function is used to change the owner of a file. It can only be used if the script is
running as root, which should never happen.
Advanced PHP Techniques
P
ART IV
364
21 7842 CH16 3/6/01 3:40 PM Page 364










