Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP filesize(), filemtime() and stat() functions

Reference for PHP filesize(), filemtime(), fileperms(), mime_content_type() and stat(): size, timestamps and permissions of a file, stat caching and failure behavior.

A group of functions answers questions about an existing file without opening it. filesize() returns the size in bytes, filemtime() the last modification time as a Unix timestamp, fileperms() the permission bits, and stat() returns all of that and more in one array.

They read information from the filesystem, so they fail with a warning when the path does not exist. PHP caches the result per path for the duration of the request; call clearstatcache() when the file changes while your script runs.

PHP
$bytes = filesize("report.pdf");
$age   = time() - filemtime("report.pdf");

Try it

PHP Size, type and modification time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP The stat() array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
filesize(string $filename): int|false
filemtime(string $filename): int|false
fileperms(string $filename): int|false
filetype(string $filename): string|false
stat(string $filename): array|false
clearstatcache(bool $clear_realpath_cache = false, string $filename = ""): void
Name Description
$filename Path of an existing file or directory.

Return value: the requested value, or false (with an E_WARNING) if the path does not exist or cannot be examined. stat() returns an array with both numeric and named keys such as size, mtime, atime, ctime, mode, uid and gid.

Usage notes

  • filemtime() is a timestamp, the number of seconds since 1970. Compare with time() or format with date()/gmdate().
  • filectime() is not creation time on Unix: it is the last metadata change. Few filesystems expose a true creation time.
  • Stat cache. After you change a file, the old size can still be returned for the same path; call clearstatcache(), as the first example shows.
  • Open handles: fstat($handle) returns the same information for an open stream and is always current.
  • Large files: on 32-bit builds, filesize() can be wrong above 2 GB; 64-bit PHP is fine.
  • Directories have a size too, but it is filesystem-specific and not the total of their contents.
  • fileperms() returns the mode as an integer; show it with substr(sprintf("%o", $mode), -4).
  • MIME types: mime_content_type() needs the fileinfo extension; do not rely on file extensions for security checks.
  • Check existence first with file_exists() when a missing file is a normal case.