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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

256

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

gfg.txt : 256 bytes

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.