Changes

Working with Directories in PHP

9 bytes added, 16:57, 13 May 2009
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 directories, deleting existing directories and list listing the contents of a directory.
== Creating Directories in PHP ==
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 a new directory in a different directory specify the full path when calling ''mkdir()''.
A second, optional argument allows the specification of permissions on the directory (controlling such issues as whether the directory is writable):
== Finding and Changing the Current Working Directory ==
It is unlikely that a web application will be able to perform all of its file related tasks in a single directory. For this reason, it is vital to be able to both find out the current working directory, and change to another directory from within a PHP striptscript.
The current working directory can be identified using the ''getCwd()'' function: