PHP Filesystem filesize() Function
The PHP Filesystem filesize() function is used to get the size of a file in bytes. After receiving the file path as input, it returns the file size in bytes as an integer.
When you need to confirm a file's size before making any changes to it, this tool is helpful. For example, you can use it to make sure that an uploaded file is not too large before processing it further.
Syntax
Below is the syntax of the PHP Filesystem filesize() function −
int filesize ( string $filename )
Parameters
The parameter is needed to use the filesize() function mentioned below −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
filename(Required) The file that will be used to get the size. |
Return Value
This function can return the size of a file in bytes or false (and can generate an error of level E_WARNING) in case of an error. FALSE on failure.
PHP Version
The filesize() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.
Example
The below PHP code is used to get the file size of the mentioned file path with the help of the PHP Filesystem filesize() function and it will return the size in bytes.
<?php //Path to the file to be used $filename = "/PhpProject/sample.txt"; echo $filename . ': ' . filesize($filename) . ' bytes'; ?>
Output
This will produce the following result −
/PhpProject/sample.txt: 27 bytes
Example
The below PHP example shows us how to use filesize() for checking the size of multiple files in a directory. So to get the list of files in a directory we will use scandir() function.
<?php
// Define the directory path
$directory = "/Applications/XAMPP/xamppfiles/htdoc/mac";
$totalSize = 0;
$files = scandir($directory);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$filePath = $directory . '/' . $file;
$totalSize += filesize($filePath);
}
}
echo "Total size of files in directory: " . $totalSize . " bytes";
?>
Output
This will generate the below result −
Total size of files in directory: 164 bytes
Example
This PHP example shows us how we can use the filesize() function to limit the size of files uploaded to a website.
<?php
$maxFileSize = 5 * 1024 * 1024; // 5 MB
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
if (filesize($_FILES['file']['tmp_name']) <= $maxFileSize) {
// Process the uploaded file
} else {
echo "Error: File size exceeds the allowed limit.";
}
} else {
echo "Error uploading file.";
}
?>
Output
This will create the below outcome −
# If the uploaded file size is within the permitted limit, the function will run the file. # If the uploaded file size exceeds what is permitted, this message will show up: "Error: File size exceeds the allowed limit." # If there is an issue with the file upload, this message will show up: "Error uploading file."
Example
In the below PHP code example we will see how to monitor disk space usage on a server with the help of filesize() function.
<?php
$directories = array("/var/www/html", "/home/user/uploads");
$totalDiskSpaceUsed = 0;
foreach ($directories as $dir) {
$files = scandir($dir);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$filePath = $dir . '/' . $file;
$totalDiskSpaceUsed += filesize($filePath);
}
}
}
echo "Total disk space used: " . $totalDiskSpaceUsed . " bytes";
?>
Output
This will lead to the following outcome −
Total disk space used: 1680 bytes
Summary
The PHP method filesize() returns the file's size in bytes. It takes a file path as an argument and returns the file size in bytes as an integer. The examples we have seen in this chapter demonstrate how to use PHP's filesize() function in applications like file size checking.