The filesize Function

The filesize function returns the file size in bytes. The function accepts the file path as a parameter. If the file does not exist, the function will return false and generate a warning.

Syntax

phpfilesize(filename);

Example

Get the size of the file 'test.txt':

php<?php
  $res = filesize('test.txt');
  echo $res;
?>

Code execution result (for example):

php1024

Example

Check if the file exists before getting its size:

php<?php
  $filename = 'test.txt';
  
  if (file_exists($filename)) {
    $res = filesize($filename);
    echo "File size: " . $res . " bytes";
  } else {
    echo "File not found";
  }
?>

Code execution result:

php'File size: 1024 bytes'

Example

Get the file size and convert it to kilobytes:

php<?php
  $res = filesize('test.txt') / 1024;
  echo round($res, 2) . " KB";
?>

Code execution result:

php'1.00 KB'

See Also