Backend Development 5 min read

Using PHP's filesize() Function to Retrieve File Size

This tutorial explains PHP's filesize() function, its syntax, usage examples, and important considerations such as local file limitations and converting byte sizes, providing clear code samples for developers, including error handling and file existence checks.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's filesize() Function to Retrieve File Size

In PHP development, we often need to obtain file size information, and PHP provides a convenient function for this purpose— filesize() . The filesize() function is used to get the size of a specified file, returning the size in bytes. This article details the usage of the filesize() function and provides concrete code examples.

Usage:

The usage of filesize() is very simple. Below is its basic syntax:

<code>filesize(string $filename): int|false</code>

Here, $filename specifies the path of the file whose size is to be obtained. The function returns the file size, or false on failure.

Code Example:

Below is an actual code example showing how to use filesize() to get a file's size:

<code>$filename = 'file.txt';

// Check if the file exists
if (file_exists($filename)) {
    $filesize = filesize($filename);
    echo '文件 ' . $filename . ' 的大小为 ' . $filesize . ' 字节。';
} else {
    echo '文件 ' . $filename . ' 不存在。';
}
</code>

In the example, we first specify the file path, use file_exists() to check whether the file exists. If it exists, we call filesize() to obtain the size and print it; otherwise we print an appropriate error message.

Notes:

When using filesize() , keep the following points in mind:

filesize() only works for local files and does not support retrieving the size of network files.

To obtain the size of a network file, use other methods such as the cURL library or sending an HTTP HEAD request.

File size is returned in bytes; to convert it to more human‑readable units (e.g., KB, MB), simple calculations can be applied.

Conclusion:

The filesize() function is a convenient way in PHP to obtain file size information, providing a simple method for file operations. After reading this article, you should have a clearer understanding of how to use filesize() and be able to apply it flexibly in real development scenarios.

PHP Learning Recommendations:

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

backendTutorialfile sizefilesize
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.