Categories
PHP

Reading Information About Files

Learn how to get all file information, for example, get modification time with filemtime(), get last access time with fileatime(), get group id with filegroup(), get user id with fileowner(), get size with filesize() and use the stat() function to get all information (meta data) about a file.

PHP offers a variety of functions that provide information about a file. The following list shows the most relevant functions in this regard:

fileatime()

<?php
 //Syntax
 fileatime(string $filename): int|false

The fileatime() function returns the time of last access of a file or false on error.

<?php
 $atime = fileatime('file.txt');
 if ($atime !== flase)
  echo 'Last access: '. $atime;
 else
  echo 'Error'; 

// Prints: Last access: 1671484767

The time is returned as a Unix timestamp, use the Date function to convert the timestamp into a human-readable form:

 echo Date('h:i:s, d-M-Y', $atime);
 // Prints: 10:19:27, 19-Dec-2022

filegroup()

<?php
 //Syntax
 filegroup(string $filename): int|false

The filegroup() function returns the group ID of the group owning the file path or false on error.

<?php
 $group = filegroup('file.txt');
 if ($group !== false)
  echo 'group ID: '. $group;
 else
  echo 'Error';

filemtime()

<?php
 //Syntax
 filemtime(string $filename): int|false

The filemtime() function returns a file’s last modification time or false on failure.

<?php
 $mtime = filemtime('file.txt');
 if ($mtime !== false)
  echo $mtime;
 else
  echo 'Error'; 

 //Prints Unix timestamp: 1671484767

Use the Date function to convert the Unix timestamp into human-readable form:

<?php
 echo Date('h:i:s, d-M-Y', $mtime);
 // 10:19:27, 19-Dec-2022

fileowner()

<?php
 //Syntax
 fileowner(string $filename): int|false

The fileowner() function returns the user ID of the owner of a file, or false if an error occurs.

<?php
 $owner = fileowner ('file.txt');
 if ($owner !== false)
  echo 'User ID: '. $owner;
 else
  echo 'Error';

filesize()

<?php
 //Syntax
 filesize(string $filename): int|false

The filesize()function returns a file’s size in bytes.

<?php
 echo filesize ('file.txt'); // 39421
 echo filesize ('/home/htdocs/img.png'); //189399

stat() – read a file’s metadata

<?php
 //Syntax
 stat(string $filename): array|false

You can also use the stat() function to get similar data with a single function call, it returns an array of information about a file:

<?php
 $file = 'file.txt';
 $meta = stat($file);
 echo '<pre>';
 print_r($meta);

//Prints:
Array
(
    [0] => 2065
    [1] => 136068100
    [2] => 33188
    [3] => 1
    [4] => 2371
    [5] => 2366
    [6] => 0
    [7] => 3236
    [8] => 1671812782
    [9] => 1591680310
    [10] => 1670696965
    [11] => 4096
    [12] => 8
    [dev] => 2065
    [ino] => 136068100
    [mode] => 33188
    [nlink] => 1
    [uid] => 2371
    [gid] => 2366
    [rdev] => 0
    [size] => 3236
    [atime] => 1671812782
    [mtime] => 1591680310
    [ctime] => 1670696965
    [blksize] => 4096
    [blocks] => 8
)

The stat() function returns an array with both numeric and string indexes with information about a file. The following values are returned by this function:

NumericAssociativeDescription
0devdevice number
1inoinode number
2modeinode protection mode (see file permissions)
3nlinknumber of links
4uiduserid of owner (return 0 on Windows)
5gidgroupid of owner (return 0 on Windows)
6rdevdevice type, if inode device
7sizefile size in bytes
8atimetime of last access (Unix timestamp)
9mtimetime of last modification (Unix timestamp)
10ctimetime of last inode change (Unix timestamp)
11blksizeblocksize of filesystem IO (return -1 on Windows)
12blocksnumber of 512-byte blocks allocated (return -1 on Windows)

Working with Files in PHP: