Saturday, November 15, 2025
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
32402 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6768 POSTS0 COMMENTS
Nicole Veronica
11919 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11990 POSTS0 COMMENTS
Shaida Kate Naidoo
6897 POSTS0 COMMENTS
Ted Musemwa
7149 POSTS0 COMMENTS
Thapelo Manthata
6850 POSTS0 COMMENTS
Umr Jansen
6841 POSTS0 COMMENTS