0
0
PHPprogramming~20 mins

File existence and info checks in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Info Master
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 checking file existence?
Consider the following PHP code snippet that checks if a file exists and prints a message accordingly. What will be the output if the file 'example.txt' does not exist in the current directory?
PHP
<?php
if (file_exists('example.txt')) {
    echo 'File found';
} else {
    echo 'File not found';
}
?>
AFile not found
BWarning: file_exists(): Unable to access example.txt
CFatal error: Uncaught Error
DFile found
Attempts:
2 left
💡 Hint
file_exists() returns false if the file is not present.
Predict Output
intermediate
2:00remaining
What does this PHP code output about file size?
Given the PHP code below, what will be the output if the file 'data.log' exists and its size is 2048 bytes?
PHP
<?php
$size = filesize('data.log');
echo "Size: $size bytes";
?>
ASize: 2048 bytes
BWarning: filesize(): stat failed for data.log
CSize: 0 bytes
DSize: bytes
Attempts:
2 left
💡 Hint
filesize() returns the size in bytes of the file if it exists.
🔧 Debug
advanced
2:00remaining
Why does this PHP code checking if a path is a directory fail?
The following PHP code is intended to check if 'myfolder' is a directory and print a message. However, it always prints 'Not a directory' even when 'myfolder' exists and is a directory. What is the cause?
PHP
<?php
if (is_dir('myfolder/')) {
    echo 'It is a directory';
} else {
    echo 'Not a directory';
}
?>
AThe directory 'myfolder' does not have read permissions
BThe trailing slash in 'myfolder/' causes is_dir() to fail
CThe code is correct; the problem is elsewhere
DThe function is_dir() requires an absolute path, not relative
Attempts:
2 left
💡 Hint
Check file system permissions for the directory.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in PHP file info check?
Which of the following PHP code snippets will cause a syntax error when checking if a file is readable?
A<?php if (is_readable('file.txt')) { echo 'Readable'; } ?>
B<?php if is_readable('file.txt') { echo 'Readable'; } ?>
C<?php if (is_readable('file.txt')) echo 'Readable'; ?>
D<?php if (is_readable('file.txt')) { echo 'Readable'; } else { echo 'Not readable'; } ?>
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
🚀 Application
expert
2:00remaining
How many items are in the array returned by this PHP file info code?
This PHP code uses stat() to get file info. How many elements does the array returned by stat('test.txt') contain?
PHP
<?php
$info = stat('test.txt');
echo count($info);
?>
A13
B15
C12
D26
Attempts:
2 left
💡 Hint
stat() returns an array with both numeric and associative keys.