PHP offers a variety of functions that provide information about a file. The following list shows the most relevant functions in this regard:
fileatime($filename)
Last access to the filefilegroup($filename)
Group that owns the filefilemtime($filename)
Last change to the filefileowner($filename)
File ownerfilesize($filename)
Size of the filestat($filename)
Gives information about a file
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:
Numeric | Associative | Description |
---|---|---|
0 | dev | device number |
1 | ino | inode number |
2 | mode | inode protection mode (see file permissions) |
3 | nlink | number of links |
4 | uid | userid of owner (return 0 on Windows) |
5 | gid | groupid of owner (return 0 on Windows) |
6 | rdev | device type, if inode device |
7 | size | file size in bytes |
8 | atime | time of last access (Unix timestamp) |
9 | mtime | time of last modification (Unix timestamp) |
10 | ctime | time of last inode change (Unix timestamp) |
11 | blksize | blocksize of filesystem IO (return -1 on Windows) |
12 | blocks | number of 512-byte blocks allocated (return -1 on Windows) |
Working with Files in PHP:
- Returning or Downloading Files with an HTTP Request
- Reading a File into a String or Array
- PHP Opening and Closing Files
- Reading files by line or by character
- Writing and appending to files
- Reading and Writing CSV Files
- Parsing INI Files and Strings
- Check File Type (whether a file is a directory or a file)
- Understanding file Permissions in PHP
- Reading Information About Files in PHP
- Copying, Moving and Deleting Files in PHP
- Reading Directories Contents in PHP
- Browse directories recursively
- Zipping and Unzipping a File with Zlib and Bzip2
- Zip and Unzip Archives with ZipArchive
- Using Relative Paths for File Access