PHP can tell you a lot about a file without opening it.
Size and dates
<?php
file_put_contents("report.txt", "Quarterly numbers");
echo filesize("report.txt") . " bytes\n";
echo date("Y", filemtime("report.txt")) === date("Y") ? "modified this year" : "older";
17 bytes
modified this year
filesize($file): size in bytes.filemtime($file): last modified time, as a Unix timestamp.fileatime()andfilectime(): last access and last change of the file’s metadata.
Name, folder and extension
<?php
$path = "/var/uploads/2026/photo.final.JPG";
print_r(pathinfo($path));
echo strtolower(pathinfo($path, PATHINFO_EXTENSION));
Array
(
[dirname] => /var/uploads/2026
[basename] => photo.final.JPG
[extension] => JPG
[filename] => photo.final
)
jpg
pathinfo() works on a string. It does not look at the real file.
A readable size
<?php
function humanSize(int $bytes): string
{
$units = ["B", "KB", "MB", "GB"];
$i = 0;
while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 1) . " " . $units[$i];
}
echo humanSize(900) . "\n";
echo humanSize(5242880) . "\n";
echo humanSize(1610612736);
900 B
5 MB
1.5 GB
The real file type: finfo
The extension is only a name. A file called photo.jpg might contain anything. To find out what the content really is, use the finfo class (the fileinfo extension).
<?php
// the smallest valid PNG image
$png = base64_decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==");
file_put_contents("fake.jpg", $png); // a PNG pretending to be a JPG
$finfo = new finfo(FILEINFO_MIME_TYPE);
echo $finfo->file("fake.jpg");
image/png
The name says .jpg, but the content is a PNG. finfo looks at the bytes, so you cannot be fooled by a rename. You will use this in Secure File Uploads.
Image size
<?php
$png = base64_decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==");
file_put_contents("tiny.png", $png);
[$width, $height] = getimagesize("tiny.png");
echo "$width x $height";
1 x 1
getimagesize() also returns false if the file is not a valid image, which makes it a useful extra check.
Clear the file status cache
PHP caches some results (filesize, file_exists) during one script. If the file changes while the script runs, call clearstatcache() before checking again.
Do not trust extensions
pathinfo() for the name, and finfo for what the file really is.