Bird
Raised Fist0
PHPprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which PHP function checks if a file or directory exists at a given path?
easy
A. filesize()
B. is_file()
C. file_exists()
D. is_readable()

Solution

  1. Step 1: Understand the purpose of file_exists()

    The function file_exists() returns true if the file or directory exists at the specified path.
  2. Step 2: Compare with other functions

    Functions like is_file() check if the path is a file, filesize() returns size, and is_readable() checks permissions, but none check existence alone.
  3. Final Answer:

    file_exists() -> Option C
  4. Quick Check:

    Check existence = file_exists() [OK]
Hint: Use file_exists() to check any file or directory presence [OK]
Common Mistakes:
  • Confusing is_file() with existence check
  • Using filesize() to check existence
  • Assuming is_readable() means file exists
2. Which of the following is the correct syntax to check if a path is a directory in PHP?
easy
A. is_directory($path);
B. is_dir($path);
C. file_is_dir($path);
D. dir_exists($path);

Solution

  1. Step 1: Recall the correct function name

    PHP uses is_dir() to check if a path is a directory.
  2. Step 2: Verify syntax correctness

    The function is called as is_dir($path); other options are invalid function names.
  3. Final Answer:

    is_dir($path); -> Option B
  4. Quick Check:

    Directory check = is_dir() [OK]
Hint: Remember: is_dir() checks directories, not is_directory() [OK]
Common Mistakes:
  • Using non-existent functions like is_directory()
  • Mixing up is_file() and is_dir()
  • Missing parentheses in function call
3. What will the following PHP code output if the file 'example.txt' exists and is 150 bytes in size?
$file = 'example.txt';
if (file_exists($file)) {
    echo filesize($file);
} else {
    echo 'File not found';
}
medium
A. 150
B. Error
C. 0
D. File not found

Solution

  1. Step 1: Check file existence

    The code uses file_exists() to confirm 'example.txt' exists, which it does.
  2. Step 2: Output file size

    Since the file exists, filesize($file) returns 150, which is printed.
  3. Final Answer:

    150 -> Option A
  4. Quick Check:

    File exists, output size = 150 [OK]
Hint: filesize() returns size only if file_exists() is true [OK]
Common Mistakes:
  • Assuming filesize() works without checking existence
  • Confusing filesize() output with file content
  • Expecting 'File not found' when file exists
4. Identify the error in this PHP code snippet that checks if a file is writable:
$filename = 'data.txt';
if (is_writeable($filename)) {
    echo 'Writable';
} else {
    echo 'Not writable';
}
medium
A. Missing semicolon after echo statements
B. is_writeable() does not check permissions
C. $filename should be in double quotes
D. The function name is misspelled; should be is_writable()

Solution

  1. Step 1: Check function spelling

    The correct function to check if a file is writable is is_writable(), not is_writeable().
  2. Step 2: Verify other syntax

    Semicolons are present, variable quotes are valid, and is_writable() does check permissions.
  3. Final Answer:

    The function name is misspelled; should be is_writable() -> Option D
  4. Quick Check:

    Correct spelling = is_writable() [OK]
Hint: Remember: writable has one 'e' after writ [OK]
Common Mistakes:
  • Misspelling is_writable() as is_writeable()
  • Thinking variable needs double quotes
  • Confusing permission check with existence
5. You want to list all files in a directory 'docs/' that exist, are readable, and have been modified in the last 24 hours. Which PHP code snippet correctly implements this?
hard
A. $files = scandir('docs/'); foreach ($files as $file) { $path = 'docs/' . $file; if (file_exists($path) && is_readable($path) && (time() - filemtime($path) < 86400)) { echo $file . "\n"; } }
B. $files = scandir('docs/'); foreach ($files as $file) { if (is_dir($file)) continue; if (is_writable($file)) { echo $file . "\n"; } }
C. $files = scandir('docs/'); foreach ($files as $file) { if (file_exists($file) && filesize($file) > 0) { echo $file . "\n"; } }
D. $files = scandir('docs/'); foreach ($files as $file) { if (is_file($file) && is_readable($file)) { echo $file . "\n"; } }

Solution

  1. Step 1: Understand requirements

    We need to list files in 'docs/' that exist, are readable, and modified within last 24 hours (86400 seconds).
  2. Step 2: Analyze each option

    $files = scandir('docs/'); foreach ($files as $file) { $path = 'docs/' . $file; if (file_exists($path) && is_readable($path) && (time() - filemtime($path) < 86400)) { echo $file . "\n"; } } correctly scans 'docs/', builds full path, checks file_exists(), is_readable(), and filemtime() against current time minus 86400 seconds. Other options miss one or more conditions or use wrong paths.
  3. Final Answer:

    Option A code snippet -> Option A
  4. Quick Check:

    Check existence + readability + recent mod = $files = scandir('docs/'); foreach ($files as $file) { $path = 'docs/' . $file; if (file_exists($path) && is_readable($path) && (time() - filemtime($path) < 86400)) { echo $file . "\n"; } } [OK]
Hint: Combine file_exists(), is_readable(), and filemtime() with full path [OK]
Common Mistakes:
  • Not using full path when checking files
  • Ignoring file modification time condition
  • Checking is_writable() instead of is_readable()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard