What is the recommended way to calculate the size of a file using PHP?

To calculate the size of a file in PHP, you can use the filesize() function. This function takes the file path as a parameter and returns the size of the file in bytes. You can then convert the size to a more readable format if needed.

$file = 'path/to/your/file.txt';
$fileSize = filesize($file);

echo "Size of file: " . $fileSize . " bytes";