0
0
PHPprogramming~5 mins

File existence and info checks in PHP

Choose your learning style9 modes available
Introduction

We check if a file exists and get information about it to avoid errors and handle files safely.

Before opening a file to read or write, to make sure it is there.
To check if a file is a regular file or a directory before processing.
To get the size of a file before downloading or displaying it.
To verify if a file is readable or writable before performing actions.
To check the last modified time of a file for updates.
Syntax
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.

Examples
Checks if 'example.txt' exists and prints a message.
PHP
<?php
if (file_exists('example.txt')) {
    echo 'File exists';
} else {
    echo 'File does not exist';
}
?>
Checks if the path is a file or directory and prints the result.
PHP
<?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';
}
?>
Gets the size of 'example.txt' and prints it if available.
PHP
<?php
$size = filesize('example.txt');
if ($size !== false) {
    echo "File size: $size bytes";
} else {
    echo 'Cannot get file size';
}
?>
Sample Program

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
<?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";
}
?>
OutputSuccess
Important Notes

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.

Summary

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.