Wednesday, October 8, 2025
HomeLanguagesPHP | filesize( ) Function

PHP | filesize( ) Function

The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.

The result of the filesize() function is cached and a function called clearstatcache() is used to clear the cache.

Syntax:

filesize($filename)

Parameters: The filesize() function in PHP accepts only one parameter $filename. It specifies the filename of the file whose size you want to check.

Return Value: It returns the size of a file in bytes on success and False on failure.

Errors And Exception:

  1. For files which are larger than 2GB some filesystem functions may return unexpected results since PHP’s integer type is signed and many platforms use 32bit integers.
  2. The buffer must be cleared if the filesize() function is used multiple times.
  3. The filesize() function emits an E_WARNING in case of a failure.

Examples:


Input : echo filesize("gfg.txt");
Output : 256

Input : $myfile = 'gfg.txt';
        echo $myfile . ': ' . filesize($myfile) . ' bytes';
Output : gfg.txt : 256 bytes

Below programs illustrate the filesize() function.

Program 1:




<?php
  
// displaying file size using
// filesize() function
echo filesize("gfg.txt");
  
?>


Output:

256

Program 2:




<?php
  
// displaying file size using
// filesize() function
$myfile = 'gfg.txt';
  
echo $myfile . ': ' . filesize($myfile) . ' bytes';
  
?>


Output:

gfg.txt : 256 bytes

Reference:
http://php.net/manual/en/function.filesize.php

RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS