Open In App

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
    
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
    
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
Article Tags :