PHP Working with Directories – Create, Read & Manage
PHP provides a rich set of functions to create, read, update, and delete directories. Here’s a beginner-friendly guide to working with directories in PHP.
1. Creating Directories
Use the mkdir() function to create a new directory.
Example: Creating a Directory
<?php
$dirName = "example_dir";
if (!is_dir($dirName)) {
if (mkdir($dirName)) {
echo "Directory '$dirName' created successfully.";
} else {
echo "Failed to create directory.";
}
} else {
echo "Directory already exists.";
}
?>
2. Listing Directory Contents
To read the contents of a directory, use scandir() or opendir() and readdir().
Example: Listing Files with scandir()
<?php
$dirName = "example_dir";
if (is_dir($dirName)) {
$files = scandir($dirName);
foreach ($files as $file) {
if ($file != "." && $file != "..") {
echo $file . "<br>";
}
}
} else {
echo "Directory does not exist.";
}
?>
Example: Using opendir()
<?php
$dirName = "example_dir";
if ($handle = opendir($dirName)) {
while (($file = readdir($handle)) !== false) {
if ($file != "." && $file != "..") {
echo $file . "<br>";
}
}
closedir($handle);
} else {
echo "Failed to open directory.";
}
?>
3. Deleting Directories
Use rmdir() to remove an empty directory. To delete a directory with files, you need a recursive function.
Example: Deleting an Empty Directory
<?php
$dirName = "example_dir";
if (is_dir($dirName)) {
if (rmdir($dirName)) {
echo "Directory '$dirName' deleted successfully.";
} else {
echo "Failed to delete directory.";
}
} else {
echo "Directory does not exist.";
}
?>
Example: Deleting a Directory with Files
<?php
function deleteDirectory($dir) {
if (!is_dir($dir)) return false;
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$filePath = $dir . DIRECTORY_SEPARATOR . $file;
is_dir($filePath) ? deleteDirectory($filePath) : unlink($filePath);
}
return rmdir($dir);
}
$dirName = "example_dir";
if (deleteDirectory($dirName)) {
echo "Directory '$dirName' deleted successfully.";
} else {
echo "Failed to delete directory.";
}
?>
4. Checking Directory Properties
You can check if a directory exists, is readable, or is writable using is_dir(), is_readable(), and is_writable().
Example: Checking Directory Properties
<?php
$dirName = "example_dir";
if (is_dir($dirName)) {
echo "Directory exists.<br>";
echo is_readable($dirName) ? "Directory is readable.<br>" : "Directory is not readable.<br>";
echo is_writable($dirName) ? "Directory is writable.<br>" : "Directory is not writable.<br>";
} else {
echo "Directory does not exist.";
}
?>
5. Getting Directory Path Information
Use basename(), dirname(), and realpath() to retrieve directory information.
Example: Getting Directory Information
<?php $path = "/var/www/html/example_dir"; echo "Directory name: " . basename($path) . "<br>"; echo "Parent directory: " . dirname($path) . "<br>"; echo "Absolute path: " . realpath($path) . "<br>"; ?>
6. Changing Current Directory
Use chdir() to change the working directory in a script.
Example: Changing Directory
<?php
$dirName = "example_dir";
if (chdir($dirName)) {
echo "Current directory is now: " . getcwd();
} else {
echo "Failed to change directory.";
}
?>
7. Summary of Directory Functions
| Function | Description |
|---|---|
| mkdir() | Creates a new directory. |
| rmdir() | Removes an empty directory. |
| scandir() | Lists the contents of a directory. |
| opendir() | Opens a directory handle. |
| readdir() | Reads a file/directory from the handle. |
| closedir() | Closes a directory handle. |
| is_dir() | Checks if a file path is a directory. |
| is_readable() | Checks if a directory is readable. |
| is_writable() | Checks if a directory is writable. |
| chdir() | Changes the current working directory. |
| getcwd() | Gets the current working directory. |