Saturday, February 21, 2026
HomeLanguagesHow to get the file size using PHP ?

How to get the file size using PHP ?

In this article, we are going to discuss how to get the file size using PHP. PHP is a general-purpose scripting language that is suited for both server and client scripting language for website development. To get the file size, we will use filesize() function. 

The filesize() function returns the size of a file in bytes. This function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure. 

Syntax:

filesize($filename)

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

Example 1: In this example, we use filesize() function with a file parameter and it returns the size of given file.

PHP




<?php
    
echo "The File size is: ";
  
echo filesize("gfg.txt");
  
?>


Output:

The File size is: 15

Example 2: In this example, we use filesize() function with a non-existing file parameter then it returns an error message.

PHP




<?php
    
echo "The File size is: ";
  
echo filesize("gfg2.txt");
?>


Output:

The File size is: 
Warning: filesize(): stat failed for inde1x.php in
C:\xampp\htdocs\gfg.php on line 4
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS