ItSolutionStuff.com

How to Get the File Size in PHP?

By Hardik Savani • May 14, 2024
PHP

Hi Artisan,

In this quick example, let's see how to get file size in php. you can understand a concept of get image size in php. This tutorial will give you a simple example of check image size in php. This article goes in detailed on get image size in bytes php.

Example 1:

In PHP, you can get the size of a file using the filesize() function. Here's an example code snippet:

Code:

<?php

$file = 'path/to/your/file.csv';

$fileSize = filesize($file);

echo "The size of the file is: " . $fileSize . " bytes";

?>

In this example, the filesize() function is used to get the size of the file at the specified path. The result is then stored in the $fileSize variable, and printed to the screen using the echo statement.

Example 2:

Note that the filesize() function returns the size of the file in bytes. If you want to display the size in a more human-readable format (such as kilobytes or megabytes), you can use some additional code to convert the result. Here's an example:

Code:

<?php

$fileSize = filesize($file);

$humanSize = round($fileSize / 1024 / 1024, 2) . ' MB';

echo "The size of the file is: " . $humanSize;

?>

In this example, the filesize() function is called as before, but the result is then divided by 1024 twice to convert it from bytes to megabytes. The round() function is used to round the result to two decimal places, and the result is then concatenated with the string 'MB' to create a human-readable file size.

I hope it can help you...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Ajax Multiple Image Upload with Preview Example

Read Now →

PHP MySQL Highcharts Chart Example

Read Now →

PHP Behance API Tutorial with Example

Read Now →

PHP Import Excel File into MySQL Database Tutorial

Read Now →

How to Integrate Google Recaptcha with PHP Form?

Read Now →

How to Get File Name without Extension in PHP?

Read Now →

How to Remove White Space from String in PHP?

Read Now →

How to Get a Current Page URL in PHP?

Read Now →

How to Increase Upload File Size Limit PHP in Ubuntu?

Read Now →

How to Check If String is URL or Not in PHP?

Read Now →

How to access PHP variables in JQuery?

Read Now →

How to Count Number of Files in a Directory in PHP?

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →