0
0
PHPprogramming~20 mins

Directory operations in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Directory Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code listing directory contents?
Consider this PHP code that reads and prints the names of files in a directory.
PHP
<?php
$dir = 'testdir';
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      echo "$file\n";
    }
    closedir($dh);
  }
}
?>
ALists only files in 'testdir' excluding '.' and '..'
BLists all files and directories including '.' and '..' in 'testdir' each on a new line
CThrows a runtime error because opendir is not used correctly
DOutputs nothing because the directory is not opened
Attempts:
2 left
💡 Hint
Remember readdir returns '.' and '..' entries by default.
Predict Output
intermediate
2:00remaining
What does this PHP code output when filtering directory files?
This PHP code lists only files (not directories) inside 'myfolder'. What is the output?
PHP
<?php
$dir = 'myfolder';
$files = [];
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if ($file !== '.' && $file !== '..' && is_file($dir . DIRECTORY_SEPARATOR . $file)) {
        $files[] = $file;
      }
    }
    closedir($dh);
  }
}
echo count($files);
?>
AThrows a warning because DIRECTORY_SEPARATOR is undefined
BPrints the total number of entries including directories inside 'myfolder'
CPrints 0 because is_file always returns false here
DPrints the number of files (not directories) inside 'myfolder'
Attempts:
2 left
💡 Hint
is_file checks if the path is a regular file, not a directory.
Predict Output
advanced
2:00remaining
What error does this PHP code raise when trying to remove a directory?
This code tries to remove a directory named 'oldfolder'. What error or output occurs?
PHP
<?php
$dir = 'oldfolder';
rmdir($dir);
echo "Removed";
?>
AWarning: rmdir(): Directory not empty if 'oldfolder' has files
BFatal error because rmdir requires a second argument
CNo output because rmdir silently fails
DRemoved printed even if directory does not exist
Attempts:
2 left
💡 Hint
rmdir only removes empty directories.
Predict Output
advanced
2:00remaining
What is the output of this PHP code creating a directory?
This PHP code tries to create a directory 'newdir' with permissions 0755. What is the output?
PHP
<?php
$dir = 'newdir';
if (mkdir($dir, 0755)) {
  echo "Created";
} else {
  echo "Failed";
}
?>
APrints nothing because mkdir returns void
BThrows a syntax error because 0755 is not a valid permission
CPrints 'Created' if directory does not exist and is created successfully
DPrints 'Failed' if directory already exists
Attempts:
2 left
💡 Hint
mkdir returns true on success, false on failure.
🧠 Conceptual
expert
2:00remaining
How many items are in the array after this PHP directory scan?
This PHP code scans a directory and filters out '.' and '..'. How many items does the array contain?
PHP
<?php
$dir = 'sampledir';
$items = scandir($dir);
$filtered = array_filter($items, fn($item) => $item !== '.' && $item !== '..');
echo count($filtered);
?>
ANumber of all files and directories inside 'sampledir' excluding '.' and '..'
BNumber of files only inside 'sampledir'
CAlways 0 because array_filter removes all items
DNumber of items including '.' and '..'
Attempts:
2 left
💡 Hint
scandir returns all entries including '.' and '..'.