34,333
edits
Changes
no edit summary
Now that we have spent some time looking at how to work with files in PHP (see [[PHP, Filesystems and File I/O]]) it is now time to look at how to work with file system directories. PHP provides a number of functions that can be used to perform tasks such as identifying and changing the current directory, creating new directorysdirectories, deleting existing directorys directories and list the contents of a directory.
A new directory can be created in PHP using the ''mkdir()'' function. This function takes a path to the directory to be created. To create a directory in the same directory as your PHP script simply provide the directory name. To create directory in a different directory specify the full path when calling ''mkdir()''.
A second, oprional optional argument allows the specification of permissions on the directory (controlling such issues as whether the directory is writable):
<pre>
</pre>
The current woreking working directory can be changed using the ''chdir()'' function. ''chdir()'' takes as the only argument the path of the new directory:
<pre>
== Listing Files in a Directory ==
The files in a directory can be read using the PHP ''scandir()'' function. ''scandir ()'' takes two arguments. The first argument is the path the directory to be scanned. The second optional argument specifies how the directory listing is to be sorted. If the argument is 1 the listing is sorted reverse-alphabetically. If the argument is omitted or set to 0 the list is sorted alphabetically:
<pre>