How to get the file size in PHP?

To get the size of a file in PHP you can useĀ filesize function of PHP. TheĀ filesize function in PHP is already inbuilt function from PHP 4. Below is the syntax of this function:


This PHP function returns the size of the given file in bytes on success or FALSE on failure. You just need to pass the file path as the filename parameter that you can see in the syntax.

Process form value in PHP using jQuery AJAX method

PHP urlencode And urldecode functions and it’s usage

The filename parameter is required and it is the path of that file whose size will be returned.

Below is a given example code snippets which will display the size of a file:

  echo filesize("filename.jpg");

If you test the code then you will see the file size of the image in bytes. The file type can be anything like a text file, audio or video file etc.

How To Redirect URL Using PHP header redirect?

Below is another example which converts the bytes value into Kilo Bytes if the file size is greater than or equal to 1024.

  $sizeInByte = filesize("filename.mp4");
  if ($sizeInByte >= 1024) {
  	$sizeInKb = $sizeInByte / 1024;
  	echo "File size is $sizeInKb Kb";
  } else {
    echo "File size is $sizeInByte bytes";

We know that 1024 bytes equal to 1 kilobytes. So we have first check if the file size is greater than or equal to 1024 then divide the byte value with 1024 and represent it as Kb or kilobytes.

Leave a Reply

Your email address will not be published. Required fields are marked *