0
0
PHPprogramming~15 mins

File existence and info checks in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
File existence and info checks
📖 Scenario: You are creating a simple PHP script to check if a file exists on the server and get some basic information about it. This is useful when you want to confirm a file is present before working with it.
🎯 Goal: Build a PHP script that checks if a file named example.txt exists, then gets its size and last modified time if it does.
📋 What You'll Learn
Create a variable called $filename with the value 'example.txt'.
Create a variable called $fileExists that stores whether the file exists using file_exists().
If the file exists, create variables $fileSize and $fileModified to store the file size and last modified time using filesize() and filemtime().
Print the results clearly showing if the file exists, and if yes, its size and last modified time.
💡 Why This Matters
🌍 Real World
Checking if files exist and getting their info is common in web apps to avoid errors when opening or processing files.
💼 Career
Many programming jobs require handling files safely and showing file details to users or logs.
Progress0 / 4 steps
1
DATA SETUP: Create the filename variable
Create a variable called $filename and set it to the string 'example.txt'.
PHP
Need a hint?

Use = to assign the string 'example.txt' to $filename.

2
CONFIGURATION: Check if the file exists
Create a variable called $fileExists and set it to the result of file_exists($filename).
PHP
Need a hint?

Use the file_exists() function with $filename as the argument.

3
CORE LOGIC: Get file size and last modified time if file exists
Write an if statement that checks if $fileExists is true. Inside it, create variables $fileSize and $fileModified and set them to filesize($filename) and filemtime($filename) respectively.
PHP
Need a hint?

Use if ($fileExists) { ... } and inside create $fileSize and $fileModified using the correct functions.

4
OUTPUT: Print the file existence and info
Write code to print if the file exists. If it does, print the file size and last modified time using echo. Format the last modified time using date('Y-m-d H:i:s', $fileModified).
PHP
Need a hint?

Use echo to print messages. Use date() to format the timestamp.