PHP Directories
ADVERTISEMENTS
Directories are predefined features of PHP, These features are used to control & make all operations on system directories.
These provide such operations like open, read, write & change directory, etc.
Example-1
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>
Output
filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: cgi : filetype: dir
filename: cli : filetype: dir
Example-2
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
/* This is the WRONG way to loop over the directory. */
while ($entry = readdir($handle)) {
echo "$entry\n";
}
closedir($handle);
}
?>
Requirements
No external libraries are needed to build this extension.
Installation
There is no installation needed to use these functions; they are part of the PHP core.
Runtime Configuration
This extension has no configuration directives defined in php.ini.
Resource Types
This extension has no resource types defined.
Predefined Constants
- DIRECTORY_SEPARATOR
- PATH_SEPARATOR Semicolon on Windows, colon otherwise.
- SCANDIR_SORT_ASCENDING Available since PHP 5.4.0.
- SCANDIR_SORT_DESCENDING Available since PHP 5.4.0.
- SCANDIR_SORT_NONE Available since PHP 5.4.0.
Directory Functions
- chdir — Change directory
- chroot — Change the root directory
- closedir — Close directory handle
- dir — Return an instance of the Directory class
- getcwd — Gets the current working directory
- opendir — Open directory handle
- readdir — Read entry from directory handle
- rewinddir — Rewind directory handle
- scandir — List files and directories inside the specified path