Challenge - 5 Problems
File Info Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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'; } ?>
Attempts:
2 left
💡 Hint
file_exists() returns false if the file is not present.
✗ Incorrect
The function file_exists() returns false if the file does not exist, so the else branch runs and prints 'File not found'.
❓ Predict Output
intermediate2: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"; ?>
Attempts:
2 left
💡 Hint
filesize() returns the size in bytes of the file if it exists.
✗ Incorrect
Since 'data.log' exists and is 2048 bytes, filesize() returns 2048 and the echo prints 'Size: 2048 bytes'.
🔧 Debug
advanced2: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'; } ?>
Attempts:
2 left
💡 Hint
Check file system permissions for the directory.
✗ Incorrect
The trailing slash in 'myfolder/' does not cause is_dir() to fail. is_dir() works with relative paths and trailing slashes. If the directory exists and permissions are correct, the code is correct. Therefore, the problem is elsewhere.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
✗ Incorrect
Option B is missing parentheses around the condition in the if statement, causing a syntax error.
🚀 Application
expert2: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); ?>
Attempts:
2 left
💡 Hint
stat() returns an array with both numeric and associative keys.
✗ Incorrect
stat() returns an array with 26 elements: 13 numeric keys and 13 associative keys with the same values.