0
0
PHPprogramming~10 mins

Directory operations in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a directory named 'files'.

PHP
<?php
$dir = [1]('files');
?>
Drag options to blanks, or click blank then click option'
Afopen
Breadfile
Copendir
Dfile_get_contents
Attempts:
3 left
💡 Hint
Common Mistakes
Using fopen which opens files, not directories.
Using readfile which reads file contents, not directories.
2fill in blank
medium

Complete the code to read an entry from the directory handle.

PHP
<?php
$dir = opendir('files');
$file = [1]($dir);
?>
Drag options to blanks, or click blank then click option'
Ascandir
Breaddir
Cfile_get_contents
Dfread
Attempts:
3 left
💡 Hint
Common Mistakes
Using fread which reads file content, not directory entries.
Using scandir which returns all entries at once, not one by one.
3fill in blank
hard

Fix the error in the code to properly close the directory handle.

PHP
<?php
$dir = opendir('files');
// some code
[1]($dir);
?>
Drag options to blanks, or click blank then click option'
Aclosedir
Bfclose
Cclosefile
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using fclose which closes file handles, not directories.
Using closefile which is not a valid PHP function.
4fill in blank
hard

Fill both blanks to create an array of files in the 'docs' directory and check if it contains 'readme.txt'.

PHP
<?php
$files = [1]('docs');
if (in_array([2], $files)) {
    echo 'Found readme.txt';
}
?>
Drag options to blanks, or click blank then click option'
Ascandir
B'readme.txt'
C'README.TXT'
Dopendir
Attempts:
3 left
💡 Hint
Common Mistakes
Using opendir which returns a directory handle, not an array.
Checking for 'README.TXT' which is case sensitive and different.
5fill in blank
hard

Fill all three blanks to loop through the 'images' directory and print each file name except '.' and '..'.

PHP
<?php
$dir = opendir('images');
while (($file = [1]($dir)) !== false) {
    if ($file != [2] && $file != [3]) {
        echo $file . "\n";
    }
}
closedir($dir);
?>
Drag options to blanks, or click blank then click option'
Areaddir
B'.'
C'..'
Dscandir
Attempts:
3 left
💡 Hint
Common Mistakes
Using scandir inside the loop which returns all files at once.
Not skipping '.' and '..' which are not actual files.