In PHP, fstat() returns information about an open file, such as its size, permissions and last modified time. It works on a file handle, not on a file name.
The result is an array that holds each value twice, once with a number and once with a name like size or mtime. Most beginners only need the named keys.
Syntax
Call fstat() with the values you need. Start with this line.
PHP
$stat = fstat($fp);
Call
What it does
fstat($fp)['size']
Returns the size of the open file in bytes.
fstat($fp)['mtime']
Returns the last modified time as a Unix timestamp.
fstat($fp)['mode']
Returns the file type and permission bits as a number.
fstat() is close to stat(), but it uses a file you have already opened. Because it reads from the open handle, it does not use the stat cache, so you do not need clearstatcache().
Library declarations
PHP
fstat(resource $stream): array|false
Parameters
Parameter
Description
stream
Required. A file handle returned by fopen() or a similar function.
Return Value
Return Value
Description
array
An array with keys such as dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize and blocks.
false
Returned when the information cannot be read.
PHP Examples
Example 1 — Read the File Size
Open a small file and ask fstat() how many bytes it holds.