Working with Directories in PHP

From Techotopia
Revision as of 22:21, 1 February 2016 by Neil (Talk | contribs) (Text replacement - "<google>BUY_PHP</google>" to "<htmlet>php<htmlet>")

Jump to: navigation, search
PreviousTable of ContentsNext
PHP, Filesystems and File I/OAn Overview of HTML Forms


Cannot find HTML file php<htmlet> Now that we have spent some time looking at how to work with files in PHP (see [[PHP, Filesystems and File IO]]) 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 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): <pre> <?php $result = mkdir ("pathtodirectory", "0777"); ?> <pre> <google>ADSDAQBOX_FLOW<google> == Deleting a Directory == Directories are deleted in PHP using the ''rmdir()'' function. ''rmdir()'' takes a single argument, the name of the directory to be deleted. The deletion will only be successful if the directory is empty. If the directory contains files or other sub-directories the deletion cannot be performed until those files and sub-directories are also deleted. == 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 script. The current working directory can be identified using the ''getCwd()'' function: <pre> <?php $current_dir = getCwd(); echo "Current directory is $current_dir"; ?> <pre> The current working directory can be changed using the ''chdir()'' function. ''chdir()'' takes as the only argument the path of the new directory: <pre> <?php $current_dir = getCwd(); echo "Current directory is $current_dir <br>"; chdir ("tmp"); $current_dir = getCwd(); echo "Current directory is now $current_dir <br>"; ?> <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> <?php chdir ("tmp"); $current_dir = getCwd(); echo "Current directory is now $current_dir"; $array = scandir(".", 1); print_r($array); ?> <pre> <htmlet>php<htmlet>.html