0
0
PHPprogramming~10 mins

File existence and info checks in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File existence and info checks
Start
Check if file exists
Yes
Get file info
Display info
End
No
Display 'File not found'
End
The program first checks if a file exists. If yes, it gets and shows file details. If not, it shows a 'File not found' message.
Execution Sample
PHP
<?php
$filename = 'example.txt';
if (file_exists($filename)) {
  echo filesize($filename);
} else {
  echo 'File not found';
}
?>
This code checks if 'example.txt' exists and prints its size or a not found message.
Execution Table
StepActionEvaluationResult
1Set $filename = 'example.txt'Assign string$filename = 'example.txt'
2Check file_exists('example.txt')Returns true if file existstrue
3Since true, call filesize('example.txt')Returns file size in bytes1234
4Print file sizeOutput file size1234
5End of scriptNo more codeScript ends
💡 Execution stops after printing file size because file exists.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$filenameundefined'example.txt''example.txt''example.txt''example.txt'
file_exists resultundefinedundefinedtruetruetrue
filesize resultundefinedundefinedundefined12341234
Key Moments - 2 Insights
Why does the code check file_exists before calling filesize?
Because calling filesize on a non-existing file causes an error. The check prevents this by ensuring the file is there first (see step 2 and 3 in execution_table).
What happens if the file does not exist?
The else branch runs, printing 'File not found' instead of trying to get file info (not shown in this example but implied by the else block).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $filename after step 1?
A'example.txt'
Bundefined
C1234
Dtrue
💡 Hint
Check the variable_tracker row for $filename at 'After Step 1'
At which step does the code decide the file exists?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'file_exists result' in variable_tracker and execution_table step 2
If the file did not exist, what would the output be?
AThe file size in bytes
BAn error message from filesize
C'File not found'
DNo output
💡 Hint
The else branch prints 'File not found' when file_exists returns false
Concept Snapshot
PHP file existence and info checks:
- Use file_exists('filename') to check if a file is present.
- Use filesize('filename') to get file size in bytes.
- Always check file_exists before filesize to avoid errors.
- Use if-else to handle file found or not found cases.
Full Transcript
This example shows how PHP checks if a file exists using file_exists. If the file is there, it gets the file size with filesize and prints it. If not, it prints 'File not found'. The execution table traces each step: setting the filename, checking existence, getting size, and printing. The variable tracker shows how variables change. Key moments explain why the existence check is important to avoid errors. The quiz tests understanding of variable values and flow decisions.