PHP Tutorial - PHP filesize() Function






Definition

The filesize() function returns the size of the specified file.

Syntax

PHP filesize() Function has the following syntax.

filesize(filename)

Parameter

ParameterIs RequiredDescription
filenameRequired.File to check

Return

This function returns the file size in bytes on success or FALSE on failure.

Note

The result of this function are cached. Use clearstatcache() to clear the cache.





Example

The filesize() function returns its filesize in bytes.

To use fread() to read in an entire file, we can use the following line:


<?PHP
      echo filesize("test.txt");
      
      $filename = "test.txt";
      $handle = fopen("data.zip", "r");
      $contents = fread($handle, filesize($filename));
?>

Each time you read in a byte, PHP advances the file pointer by one place.





Example 2

The following code shows how to find pathnames matching a pattern.



//Convenient way how glob() can replace opendir()
<?php
foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>

The code above generates the following result.