Directory operations help you work with folders on your computer using PHP. You can create, read, and list folders to organize files.
Directory operations in PHP
<?php // Open a directory $dir = opendir('path/to/directory'); // Read entries while (($file = readdir($dir)) !== false) { echo $file . "\n"; } // Close directory closedir($dir); // Create a directory mkdir('path/to/new_directory'); // Check if directory exists if (is_dir('path/to/directory')) { // do something } // Remove a directory (must be empty) rmdir('path/to/directory'); ?>
Use opendir() to open a folder and readdir() to read its contents.
mkdir() creates a new folder, and rmdir() removes an empty folder.
my_folder in the current directory.<?php
mkdir('my_folder');
?>my_folder exists and prints a message.<?php if (is_dir('my_folder')) { echo "Folder exists."; } else { echo "Folder does not exist."; } ?>
my_folder and lists all files and folders inside it.<?php $dir = opendir('my_folder'); while (($file = readdir($dir)) !== false) { echo $file . "\n"; } closedir($dir); ?>
my_folder.<?php
rmdir('my_folder');
?>This program creates a folder, checks it, lists its contents (which are just special entries), then removes it and confirms removal.
<?php // Create a folder named 'test_dir' mkdir('test_dir'); // Check if folder exists if (is_dir('test_dir')) { echo "Folder 'test_dir' created successfully.\n"; } // Open the folder $dir = opendir('test_dir'); // Since it's empty, reading will show '.' and '..' while (($file = readdir($dir)) !== false) { echo "Found entry: $file\n"; } // Close the directory closedir($dir); // Remove the folder rmdir('test_dir'); // Confirm removal if (!is_dir('test_dir')) { echo "Folder 'test_dir' removed successfully."; } ?>
The entries '.' and '..' represent the current and parent directories and always appear when reading a directory.
You cannot remove a folder with rmdir() if it contains files or other folders.
Always close directories with closedir() after opening them.
Use mkdir() to create folders and rmdir() to remove empty folders.
Use opendir(), readdir(), and closedir() to read folder contents.
Check if a folder exists with is_dir() before working with it.
Practice
Solution
Step 1: Understand directory creation functions
In PHP,mkdir()is the function designed to create new directories.Step 2: Differentiate from other directory functions
rmdir()removes directories,opendir()opens directories, andclosedir()closes them.Final Answer:
mkdir() -> Option AQuick Check:
Create directory = mkdir() [OK]
- Confusing mkdir() with rmdir()
- Using opendir() to create folders
- Trying to create folder without mkdir()
temp in PHP?Solution
Step 1: Identify the correct function for removing directories
PHP usesrmdir()to remove empty directories.Step 2: Check syntax correctness
The syntaxrmdir('temp');correctly removes the directory named 'temp'. Other options are not valid PHP functions.Final Answer:
rmdir('temp'); -> Option CQuick Check:
Remove empty folder = rmdir() [OK]
- Using non-existent functions like remove_dir()
- Trying to delete non-empty folders with rmdir() without clearing
- Syntax errors like missing quotes or semicolons
$dir = 'myfolder';
if (is_dir($dir)) {
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
echo $file . ' ';
}
closedir($handle);
} else {
echo 'Directory not found';
}Solution
Step 1: Check if directory exists
The code usesis_dir()to check if 'myfolder' exists. If it does, it proceeds to open it.Step 2: Read directory contents with readdir()
Thereaddir()function reads all entries including special entries '.' (current directory) and '..' (parent directory). These are echoed with spaces.Final Answer:
List of all files and folders inside 'myfolder' including '.' and '..' -> Option DQuick Check:
readdir() lists all entries including '.' and '..' [OK]
- Assuming '.' and '..' are excluded automatically
- Not checking if directory exists before reading
- Forgetting to close directory handle
$dir = 'testdir';
if (mkdir($dir)) {
echo 'Created';
} else {
echo 'Failed';
}
rmdir($dir);What is the main issue if the directory
testdir already exists?Solution
Step 1: Behavior of mkdir() when directory exists
Iftestdiralready exists,mkdir()returns false and echoes 'Failed'.Step 2: Behavior of rmdir() on existing directory
Since the directory exists,rmdir()will attempt to remove it. If the directory is empty and permissions allow, it will succeed. Otherwise, it will fail.Final Answer:
mkdir() will fail and echo 'Failed', but rmdir() will still remove the directory -> Option BQuick Check:
mkdir() fails if exists; rmdir() removes empty directory [OK]
- Assuming mkdir() overwrites existing folders
- Expecting rmdir() to always succeed
- Not checking if directory is empty before rmdir()
docs but skip the special entries . and ... Which PHP code snippet correctly does this?Solution
Step 1: Understand how to skip '.' and '..'
The special entries '.' and '..' appear in directory listings and must be explicitly skipped.Step 2: Analyze each code snippet
if (is_dir('docs')) { $dh = opendir('docs'); while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { echo $file . ' '; } } closedir($dh); } checks if the file is not '.' or '..' before echoing. foreach (scandir('docs') as $file) { echo $file . ' '; } uses scandir() but does not skip special entries.Final Answer:
The snippet using if ($file != '.' && $file != '..') -> Option AQuick Check:
Skip '.' and '..' with if check inside readdir() loop [OK]
- Not skipping '.' and '..' entries
- Using scandir() without filtering special entries
- Forgetting to check for false in readdir() loop
Ready to prove it?
Pass the test and this pattern moves unknown
