Bird
0
0

You want to retrieve only the regular files (not directories) from the directory reports. Which PHP code snippet correctly achieves this?

hard📝 Application Q8 of 15
PHP - File Handling
You want to retrieve only the regular files (not directories) from the directory reports. Which PHP code snippet correctly achieves this?
A$files = array_filter(scandir('reports'), function($f) { return is_file('reports/' . $f); });
B$files = scandir('reports'); foreach ($files as $f) { if (is_dir('reports/' . $f)) echo $f; }
C$files = scandir('reports'); $files = array_filter($files, 'is_dir');
D$files = scandir('reports'); foreach ($files as $f) { echo $f; }
Step-by-Step Solution
Solution:
  1. Step 1: Use scandir() to list all entries

    scandir() returns all files and directories including '.' and '..'.
  2. Step 2: Filter only files

    Use array_filter() with a callback that checks is_file() for each entry.
  3. Step 3: Correct code

    $files = array_filter(scandir('reports'), function($f) { return is_file('reports/' . $f); }); uses array_filter with is_file() correctly to get only files.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Filter with is_file() to get only files [OK]
Quick Trick: Use is_file() in array_filter to select files [OK]
Common Mistakes:
  • Using is_dir() instead of is_file()
  • Not filtering out '.' and '..'
  • Printing directories instead of files

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes