Keep reading and you’ll discover something new!
Table a
Function |
Description |
Example |
filesize ($file) |
This function returns the size of a file in bytes. It is used to calculate how much space a file occupies on disk. |
// get file owner and group |
fileatime ($file) filemtime ($file) |
These functions return the last time a file was accessed and modified respectively, and are used to find out whether a file has been modified after a specific date. |
// get file access/modification times |
fileperms ($file) |
This function returns a file permission, use it to check whether the file is readable, writable or executable. |
// get permissions in octal format |
filetype ($file) |
This function returns the "type" of the file - whether it is a connection, directory, attribute or block device, or a regular file. Use it to verify the nature of a file before performing an operation. |
// get file type |
stat($file) |
This is a "catch-all" function that returns detailed statistics of a file, including its owner and group, size, last modification time, and number of index nodes. If you need comprehensive file statistics in a single function call, use this function instead of those listed previously. |
// get file statistics |
realpath ($file) |
This function converts a relative file path to an absolute file path. This function is used when you need to find out the exact location of a file on the disk. |
// get absolute path |
basename ($file) dirname ($file) |
Given a complete file path, these functions break it into its component parts and return the file name and directory respectively. |
// split directory and file names |
file($file) |
This function reads the contents of a file into an array. Each element in the array represents a line of file. This function is used to read the contents of the file into a variable for further processing. |
// read file contents |