0
0
PHPprogramming~10 mins

File existence and info checks 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 check if the file 'example.txt' exists.

PHP
<?php
if ([1]('example.txt')) {
    echo "File exists.";
} else {
    echo "File does not exist.";
}
?>
Drag options to blanks, or click blank then click option'
Afopen
Bis_file
Cfile_get_contents
Dfile_exists
Attempts:
3 left
💡 Hint
Common Mistakes
Using fopen without checking if the file exists causes errors.
Using is_file checks if it's a file but not if it exists in all cases.
2fill in blank
medium

Complete the code to check if 'data.csv' is a regular file.

PHP
<?php
if ([1]('data.csv')) {
    echo "It's a regular file.";
} else {
    echo "It's not a regular file.";
}
?>
Drag options to blanks, or click blank then click option'
Ais_file
Bis_dir
Cfile_exists
Dis_readable
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_dir returns true only for directories.
Using file_exists returns true for files and directories.
3fill in blank
hard

Fix the error in the code to get the size of 'report.pdf'.

PHP
<?php
$size = [1]('report.pdf');
echo "File size: $size bytes.";
?>
Drag options to blanks, or click blank then click option'
Afilesizeof
Bfile_get_contents
Cfilesize
Dfile_exists
Attempts:
3 left
💡 Hint
Common Mistakes
Using file_get_contents returns the file content, not size.
Using filesizeof is not a valid PHP function.
4fill in blank
hard

Fill both blanks to check if 'image.png' is readable and writable.

PHP
<?php
if ([1]('image.png') && [2]('image.png')) {
    echo "File is readable and writable.";
} else {
    echo "File is not accessible for reading and writing.";
}
?>
Drag options to blanks, or click blank then click option'
Ais_readable
Bis_file
Cis_writable
Dfile_exists
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_file only checks if it's a file, not permissions.
Using file_exists does not check permissions.
5fill in blank
hard

Fill all three blanks to create an array with file names and their sizes for files larger than 1000 bytes in the 'docs' directory.

PHP
<?php
$files = scandir('docs');
$result = [];
foreach ($files as $file) {
    $path = 'docs/' . $[1];
    if (is_file($path) && filesize($path) [2] 1000) {
        $result[[3]] = filesize($path);
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Afile
B>
C$file
Dfilesize
Attempts:
3 left
💡 Hint
Common Mistakes
Using $files instead of $file in the path.
Using '<' instead of '>' for size comparison.
Using filesize($path) as the key instead of the file name.