Explain filesize()
function in PHP
The filesize()
function in PHP is used to get the size of a file in bytes. It is commonly used when working with file uploads or when managing files on a server. The function returns the size of the specified file in bytes, which can then be used for various purposes, such as limiting the file size for uploads or displaying the file size to users.
Syntax:
filesize(string $filename): int|false
- $filename: The path to the file for which you want to determine the size.
- Return Value: The function returns the size of the file in bytes as an integer. If the file does not exist or there is an error, it returns
false
.
Example 1: Get the size of a file
Here’s a simple example to get the size of a file and display it to the user:
<?php
$file = 'path/to/your/file.txt';
if (file_exists($file)) {
$filesize = filesize($file);
echo "The size of the file is: " . $filesize . " bytes.";
} else {
echo "File does not exist.";
}
?>
Explanation:
- The code checks if the file exists using
file_exists()
. - If the file exists, it uses
filesize()
to get the size and displays it. - If the file does not exist, it outputs an error message.
Example 2: Format the file size for user-friendly display
Since file sizes in bytes might be too large to display directly, you can format the size into a more user-friendly format such as kilobytes (KB), megabytes (MB), or gigabytes (GB).
<?php
$file = 'path/to/your/file.txt';
if (file_exists($file)) {
$filesize = filesize($file);
// Convert to a human-readable format
if ($filesize < 1024) {
$formatted_size = $filesize . ' bytes';
} elseif ($filesize < 1048576) {
$formatted_size = round($filesize / 1024, 2) . ' KB';
} elseif ($filesize < 1073741824) {
$formatted_size = round($filesize / 1048576, 2) . ' MB';
} else {
$formatted_size = round($filesize / 1073741824, 2) . ' GB';
}
echo "The size of the file is: " . $formatted_size;
} else {
echo "File does not exist.";
}
?>
Explanation:
- This example formats the file size into a human-readable format.
- Depending on the size of the file, it will display in bytes, KB, MB, or GB.
Example 3: Checking file size before upload
When allowing users to upload files, it’s important to check the file size to avoid overly large uploads that could cause performance issues.
<?php
$maxSize = 2 * 1024 * 1024; // 2MB limit
if ($_FILES['fileToUpload']['size'] > $maxSize) {
echo "Error: File is too large. Maximum file size is 2MB.";
} else {
echo "File size is acceptable.";
}
?>
Explanation:
- Here, we check the size of the uploaded file against a defined maximum size (2MB in this case).
- If the file exceeds the limit, an error message is shown; otherwise, a success message is displayed.
Key Points:
- File Size in Bytes: The size is returned in bytes, which is useful for precise calculations.
- Error Handling: Always check if the file exists before calling
filesize()
to avoid errors. - SEO Optimization: Use
filesize()
when developing file-related functionalities like file uploads, image resizing, or downloads. Optimizing file size helps enhance user experience and page load times, benefiting SEO.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.