Open In App

How to get the file size using PHP ?

Last Updated : 22 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads