The filesize() function returns the size of the specified file.
PHP filesize() Function has the following syntax.
filesize(filename)
Parameter | Is Required | Description |
---|---|---|
filename | Required. | File to check |
This function returns the file size in bytes on success or FALSE on failure.
The result of this function are cached. Use clearstatcache() to clear the cache.
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.
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.