PHP: How to get the size of a file (in bytes)

Updated: January 11, 2024 By: Guest Contributor Post a comment

Overview

Handling files is a common task in the world of programming, and sometimes you might need to know the size of a file programmatically, especially in server-side scripting languages like PHP. Knowing the size of a file can be useful for various purposes, such as data validation, user quotas, or just monitoring the storage usage. In this tutorial, we’ll explore different methods to get the size of a file using PHP, and we’ll delve into some practical examples.

Understanding File Sizes

In PHP, the size of a file is usually measured in bytes. One byte is the smallest addressable unit of data storage, meaning that it’s the smallest ‘container’ of data in a file. It’s important to understand that:

  • 1 byte = 8 bits
  • 1 kilobyte (KB) = 1024 bytes
  • 1 megabyte (MB) = 1024 kilobytes
  • 1 gigabyte (GB) = 1024 megabytes

When working with file sizes, you might need to convert between these units for readability, but for PHP functions, we will focus on bytes as the default unit.

Using filesize() Function

The easiest way to retrieve the size of a file in PHP is by using the built-in filesize() function. This function accepts a string parameter, which is the path to the file you want to check, and returns the size of the file in bytes.

If the file does not exist or there is an error reading the file, filesize() will return FALSE. To properly utilize it, it’s good practice to handle potential errors using basic error handling mechanisms.

<?php
$file = 'example.txt';
if(file_exists($file)) {
    $file_size = filesize($file);
    echo "The size of $file is $file_size bytes";
} else {
    echo "File does not exist.";
}
?>

Note that if you’re working with large files, the value returned by filesize() might exceed the maximum value that a PHP integer can hold, which depends on whether your PHP build is 32-bit or 64-bit. For 64-bit builds, the maximum integer size is quite large, so it’s unlikely that you’ll encounter this problem, but for 32-bit builds, the integer size is smaller, and you might face an overflow issue. It’s also essential to handle larger-than-integer values properly.

Handling Large Files

For large files, you can treat the data as a stream and process it in chunks, especially when the file does not fit into memory. To handle very large files, use the fopen(), fseek(), and fread() functions to read piece by piece and calculate the size manually or utilize external command line tools such as stat, which can be accessed via PHP’s exec() or shell_exec() functions.

Human-Readable Formats

Sometimes displaying file sizes in bytes isn’t user-friendly, particularly when dealing with large files. In such cases, it’s useful to convert the size into KB, MB, or GB. PHP does not have a built-in function to do this conversion, but you can easily define a function to do it.

<?php
function formatSizeUnits($bytes)
{
    if ($bytes >= 1073741824)
    {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    }
    elseif ($bytes >= 1048576)
    {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    }
    elseif ($bytes >= 1024)
    {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    }
    elseif ($bytes > 1)
    {
        $bytes = $bytes . ' bytes';
    }
    elseif ($bytes == 1)
    {
        $bytes = $bytes . ' byte';
    }
    else
    {
        $bytes = '0 bytes';
    }

    return $bytes;
}

// Use the function like this:
$file = 'example_large_file.mp4';
if(file_exists($file)) {
    $file_size = filesize($file);
    echo 'Original size: ' . $file_size . ' bytes<br>';
    echo 'Human-readable size: ' . formatSizeUnits($file_size);
} else {
    echo 'File does not exist.';
}
?>

This will print the size of the file in a more readable form depending on the size.

Important Considerations

  • Be aware of the maximum execution time of PHP scripts when working with file sizes, especially for operations that read file content.
  • File permissions can sometimes impact the ability to read a file or obtain its size. Ensure appropriate permissions are set.
  • If PHP’s safe_mode is on, you might run into ownership issues when attempting to access files.
  • Remember that using PHP to access file sizes on a remote server can be unreliable and can introduce security risks. These operations should be limited to local or trusted server files.

With this information, we’ve familiarized you with the methods of getting file sizes in bytes in PHP and converting them to more human-readable formats, as well as several important considerations to keep in mind when working with file sizes and permissions. Work on further projects will strengthen your capabilities and understanding of file operations in PHP.