Specifications
Creating and Deleting Directories
In addition to passively reading information about directories, you can use the PHP functions
mkdir() and rmdir() to create and delete directories. You will only be able to create or delete
directories in paths that the user the script runs as has access to.
Using mkdir() is more complicated than you might think. It takes two parameters, the path to
the desired directory (including the new directory name), and the permissions you would like
that directory to have, for example,
mkdir(“/tmp/testing”, 0777);
However, the permissions you list are not necessarily the permissions you are going to get. The
current umask will be ANDed (like subtraction) with this value to get the actual permissions.
For example, if the umask is 022, you will get permissions of 0755.
You might like to reset the umask before creating a directory to counter this effect, by entering
$oldumask = umask(0);
mkdir(“/tmp/testing”, 0777);
umask($oldumask);
This code uses the umask() function, which can be used to check and change the current
umask. It will change the current umask to whatever it is passed and return the old umask, or if
called without parameters, it will just return the current umask.
The rmdir() function deletes a directory, as follows:
rmdir(“/tmp/testing”);
or
rmdir(“c:\\tmp\\testing”);
The directory you are trying to delete must be empty.
Interacting with the File System
In addition to viewing and getting information about directories, we can interact with and get
information about files on the Web server. We’ve previously looked at writing to and reading
from files. A large number of other file functions are available.
Get File Info
We can alter the part of our directory browsing script that reads files as follows:
while ($file = $dir->read())
{
Interacting with the File System and the Server
C
HAPTER 16
16
INTERACTING WITH
THE
F
ILE SYSTEM
AND THE
SERVER
361
21 7842 CH16 3/6/01 3:40 PM Page 361










