274 of 410 menu

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

filesize(filename);

Example

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

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

Code execution result (for example):

1024

Example

Check if the file exists before getting its size:

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

Code execution result:

'File size: 1024 bytes'

Example

Get the file size and convert it to kilobytes:

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

Code execution result:

'1.00 KB'

See Also

byenru