File Information
File information: stat()
With stat()
you can retrieve information and statistics about a file.
The parameter is the filename and it returns an associative array with some information:
dev
: device numberino
: inode numbermode
: contains information read by several functions. When written in octal, starting from the right, thechmod()
function returns the first three digits. PHP ignores the next digit. The following two digits indicate the file typenlink
: number of linksuid
,gid
: userid and groupidrdev
: device typesize
: size in bytesatime
: last access time (unix timestamp)mtime
: last modification time (unix timestamp)ctime
: last inode change (unix timestamp)blksize
: blocksizeblocks
: number of blocks allocated
I use a lot of the mode
, uid
, gid
, size
and mtime
information.
If you have mode values in octal format you will have 7 characters.
- the first one is 0
- the second and third one is the file type
- the fourth one is 0
- the fifth, sixth and seventh are file permission
For the file type:
- 0140xxx: socket
- 0120xxx: link
- 0100xxx: regular file
- 0060xxx: block file
- 0040xxx: directory
- 0020xxx: character device
- 0010xxx: FIFO
To retrieve only the bytes for the file type you can use a bitmask, something like: $filestat['mode'] & 0xF000
I have an anonymous function to retrieve the file type in a more descriptive way, using a match constructor:
Symbolic link information
A symbolic link is a special file that links to a real file.
If you use the stat()
function on a symbolic link file, you will obtain information about the target file (the file linked by the symbolic link). However, if you want to obtain information about the symbolic link, use the lstat()
function.