PHP Directory
Working with directories and folders in PHP
📁 What is PHP Directory?
PHP Directory functions allow you to read, create, and manage folders on your server. These functions help you navigate directory structures, list files, and organize your file system efficiently.
<?php
// Open and read a directory
$dir = opendir(".");
echo readdir($dir); // Reads first entry
closedir($dir);
?>
Output:
.
Key Directory Functions
opendir()
Opens a directory handle
<?php
$dir = opendir("uploads");
?>
readdir()
Reads entries from directory
<?php
$entry = readdir($dir);
?>
closedir()
Closes directory handle
<?php
closedir($dir);
?>
scandir()
Lists files in directory
<?php
$files = scandir(".");
?>
🔹 Reading Directory Contents
The scandir() function provides an easy way to list all files and folders in a directory. It returns an array containing all entries, making it simple to loop through and display directory contents for file management tasks.
<?php
// List all files in current directory
$files = scandir(".");
foreach($files as $file) {
echo $file . "<br>";
}
?>
Output:
.
..
index.php
style.css
script.js
🔹 Creating Directories
Use mkdir() to create new directories on your server. You can set permissions and create nested directories. This function returns true on success and false on failure, allowing you to handle directory creation errors appropriately.
<?php
// Create a new directory
if(mkdir("uploads", 0777)) {
echo "Directory created successfully!";
} else {
echo "Failed to create directory.";
}
?>
Output:
Directory created successfully!
🔹 Removing Directories
The rmdir() function removes empty directories from your file system. Important: the directory must be empty before removal. Use this carefully as deleted directories cannot be recovered without backups.
<?php
// Remove an empty directory
if(rmdir("old_folder")) {
echo "Directory removed!";
} else {
echo "Cannot remove directory.";
}
?>
Output:
Directory removed!
🔹 Checking Directory Existence
Before performing operations on directories, it's good practice to verify they exist using is_dir(). This prevents errors and allows you to create directories only when needed, making your code more robust and reliable.
<?php
// Check if directory exists
if(is_dir("uploads")) {
echo "Directory exists!";
} else {
echo "Directory does not exist.";
mkdir("uploads");
}
?>
Output:
Directory exists!
🔹 Getting Current Directory
The getcwd() function returns the current working directory path. This is useful for understanding your script's location and building relative paths to other files and folders in your application structure.
<?php
// Get current working directory
$current = getcwd();
echo "Current directory: " . $current;
?>
Output:
Current directory: /var/www/html
🔹 Changing Directory
Use chdir() to change the current working directory within your PHP script. This affects all subsequent file operations, allowing you to work with files in different locations without specifying full paths every time.
<?php
// Change to different directory
if(chdir("uploads")) {
echo "Changed to: " . getcwd();
} else {
echo "Failed to change directory.";
}
?>
Output:
Changed to: /var/www/html/uploads