📖 Scenario: You are managing files on a server. You want to list all files in a folder to see what is inside.
🎯 Goal: Build a PHP script that reads the contents of a directory and prints the names of all files and folders inside it.
📋 What You'll Learn
Create a variable holding the directory path
Create a variable to open the directory using opendir()
Use a while loop with readdir() to read directory entries
Print each entry name except the special entries . and ..
💡 Why This Matters
🌍 Real World
Listing files in a folder is common when managing server files, creating file browsers, or processing multiple files automatically.
💼 Career
Understanding directory operations is important for backend developers, system administrators, and anyone working with file systems in PHP.
Progress0 / 4 steps
1
Set the directory path
Create a variable called $dir and set it to the string './test_folder'.
PHP
Hint
Use $dir = './test_folder'; to set the directory path.
2
Open the directory
Create a variable called $handle and set it to the result of opendir($dir).
PHP
Hint
Use opendir($dir) to open the directory and assign it to $handle.
3
Read directory entries
Use a while loop with readdir($handle) assigned to $entry to read each item. Inside the loop, skip entries named . and .. using an if statement.
PHP
Hint
Use while (($entry = readdir($handle)) !== false) to loop. Use if ($entry === '.' || $entry === '..') to skip special entries.
4
Print directory entries
Inside the while loop, add a print statement to display each $entry followed by a newline. After the loop, close the directory handle with closedir($handle).
PHP
Hint
Use print($entry . "\n") to show each entry. Use closedir($handle) to close the directory.
Practice
(1/5)
1. Which PHP function is used to create a new directory?
easy
A. mkdir()
B. rmdir()
C. opendir()
D. closedir()
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, and closedir() closes them.
Final Answer:
mkdir() -> Option A
Quick Check:
Create directory = mkdir() [OK]
Hint: Create folders with mkdir() function in PHP [OK]
Common Mistakes:
Confusing mkdir() with rmdir()
Using opendir() to create folders
Trying to create folder without mkdir()
2. Which of the following is the correct syntax to remove an empty directory named temp in PHP?
easy
A. delete('temp');
B. remove_dir('temp');
C. rmdir('temp');
D. rm('temp');
Solution
Step 1: Identify the correct function for removing directories
PHP uses rmdir() to remove empty directories.
Step 2: Check syntax correctness
The syntax rmdir('temp'); correctly removes the directory named 'temp'. Other options are not valid PHP functions.
Final Answer:
rmdir('temp'); -> Option C
Quick Check:
Remove empty folder = rmdir() [OK]
Hint: Use rmdir('foldername') to delete empty folders [OK]
Common Mistakes:
Using non-existent functions like remove_dir()
Trying to delete non-empty folders with rmdir() without clearing
Syntax errors like missing quotes or semicolons
3. What will be the output of the following PHP code?
What is the main issue if the directory testdir already exists?
medium
A. mkdir() will fail and echo 'Failed', and rmdir() will fail to remove the directory
B. mkdir() will fail and echo 'Failed', but rmdir() will still remove the directory
C. mkdir() will fail and echo 'Failed', and rmdir() will remove the directory anyway
D. mkdir() will overwrite the existing directory
Solution
Step 1: Behavior of mkdir() when directory exists
If testdir already 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 B
Quick Check:
mkdir() fails if exists; rmdir() removes empty directory [OK]
5. You want to list all files in a directory docs but skip the special entries . and ... Which PHP code snippet correctly does this?
hard
A. if (is_dir('docs')) {
$dh = opendir('docs');
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
echo $file . ' ';
}
}
closedir($dh);
}
B. if (is_dir('docs')) {
$dh = opendir('docs');
while (($file = readdir($dh))) {
echo $file . ' ';
}
closedir($dh);
}
C. foreach (scandir('docs') as $file) {
echo $file . ' ';
}
D. if (is_dir('docs')) {
$dh = opendir('docs');
while (($file = readdir($dh)) !== false) {
echo $file . ' ';
}
closedir($dh);
}
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 A
Quick Check:
Skip '.' and '..' with if check inside readdir() loop [OK]
Hint: Use if ($file != '.' && $file != '..') to skip special entries [OK]
Common Mistakes:
Not skipping '.' and '..' entries
Using scandir() without filtering special entries
Forgetting to check for false in readdir() loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.