We check if a file exists and get information about it to avoid errors and handle files safely.
File existence and info checks in PHP
<?php // Check if file exists file_exists(string $filename): bool; // Check if path is a file is_file(string $filename): bool; // Check if path is a directory is_dir(string $filename): bool; // Get file size in bytes filesize(string $filename): int|false; // Get last modified time (timestamp) filemtime(string $filename): int|false; // Check if file is readable is_readable(string $filename): bool; // Check if file is writable is_writable(string $filename): bool; ?>
All functions return false if the file does not exist or an error occurs.
Use these checks to avoid warnings or errors when working with files.
<?php if (file_exists('example.txt')) { echo 'File exists'; } else { echo 'File does not exist'; } ?>
<?php if (is_file('example.txt')) { echo 'It is a file'; } elseif (is_dir('example.txt')) { echo 'It is a directory'; } else { echo 'Not found'; } ?>
<?php $size = filesize('example.txt'); if ($size !== false) { echo "File size: $size bytes"; } else { echo 'Cannot get file size'; } ?>
This program checks if 'testfile.txt' exists. If yes, it prints details like size, last modified time, and permissions. If not, it says the file does not exist.
<?php $filename = 'testfile.txt'; if (file_exists($filename)) { echo "$filename exists\n"; if (is_file($filename)) { echo "$filename is a file\n"; $size = filesize($filename); echo "Size: $size bytes\n"; $modified = filemtime($filename); echo "Last modified: " . date('Y-m-d H:i:s', $modified) . "\n"; echo is_readable($filename) ? "File is readable\n" : "File is not readable\n"; echo is_writable($filename) ? "File is writable\n" : "File is not writable\n"; } elseif (is_dir($filename)) { echo "$filename is a directory\n"; } else { echo "$filename is neither file nor directory\n"; } } else { echo "$filename does not exist\n"; } ?>
Always check if a file exists before trying to open it to avoid errors.
File size and modification time help manage files efficiently.
Permissions checks (readable/writable) prevent permission errors.
Use file_exists() to check if a file or directory exists.
Use is_file() and is_dir() to know the type of path.
Use filesize(), filemtime(), is_readable(), and is_writable() to get file info and permissions.
Practice
Solution
Step 1: Understand the purpose of file_exists()
The function file_exists() returns true if the file or directory exists at the specified path.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.Final Answer:
file_exists() -> Option CQuick Check:
Check existence = file_exists() [OK]
- Confusing is_file() with existence check
- Using filesize() to check existence
- Assuming is_readable() means file exists
Solution
Step 1: Recall the correct function name
PHP uses is_dir() to check if a path is a directory.Step 2: Verify syntax correctness
The function is called as is_dir($path); other options are invalid function names.Final Answer:
is_dir($path); -> Option BQuick Check:
Directory check = is_dir() [OK]
- Using non-existent functions like is_directory()
- Mixing up is_file() and is_dir()
- Missing parentheses in function call
$file = 'example.txt';
if (file_exists($file)) {
echo filesize($file);
} else {
echo 'File not found';
}Solution
Step 1: Check file existence
The code uses file_exists() to confirm 'example.txt' exists, which it does.Step 2: Output file size
Since the file exists, filesize($file) returns 150, which is printed.Final Answer:
150 -> Option AQuick Check:
File exists, output size = 150 [OK]
- Assuming filesize() works without checking existence
- Confusing filesize() output with file content
- Expecting 'File not found' when file exists
$filename = 'data.txt';
if (is_writeable($filename)) {
echo 'Writable';
} else {
echo 'Not writable';
}Solution
Step 1: Check function spelling
The correct function to check if a file is writable is is_writable(), not is_writeable().Step 2: Verify other syntax
Semicolons are present, variable quotes are valid, and is_writable() does check permissions.Final Answer:
The function name is misspelled; should be is_writable() -> Option DQuick Check:
Correct spelling = is_writable() [OK]
- Misspelling is_writable() as is_writeable()
- Thinking variable needs double quotes
- Confusing permission check with existence
Solution
Step 1: Understand requirements
We need to list files in 'docs/' that exist, are readable, and modified within last 24 hours (86400 seconds).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.Final Answer:
Option A code snippet -> Option AQuick 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]
- Not using full path when checking files
- Ignoring file modification time condition
- Checking is_writable() instead of is_readable()
Ready to prove it?
Pass the test and this pattern moves unknown
