Get file size using filesize() function in PHP

WBOY
Release: 2023-06-27 19:02:02
Original
1393 people have browsed it

PHP is a server-side scripting language widely used in web development and is designed to provide support for the creation of dynamic web pages. One of the commonly used operations is to get the file size. File size is important to web developers as they need to ensure that the content of their website is not too large to impact the user experience.

In PHP, you can use the filesize() function to get the file size. The syntax of this function is as follows:

filesize (string $filename) : float

Where, $filename is the path of the file whose size is to be obtained. This function will return the size of the file in bytes. If the file does not exist or is inaccessible, FALSE is returned.

Next, we will introduce how to use the filesize() function in PHP to get the file size. In order to use this function, we need to prepare a file first. We can create a file using one of PHP's file manipulation functions as shown below:

<?php
$file = fopen('example.txt', 'w');
fwrite($file, 'This is an example file.');
fclose($file);
?>
Copy after login

In the above example, we created and wrote to a file named "example.txt". Now let us use filesize() function to get the size of this file.

<?php
$filename = 'example.txt';
$size = filesize($filename);
echo "The size of $filename is $size bytes.";
?>
Copy after login

In the above code, we defined a variable $filename and set it to the file name "example.txt". Then, we call the filesize() function and save its return value into a variable called $size. Finally, we use the echo statement to display the file name and file size.

If everything is fine, you should see the following output:

The size of example.txt is 23 bytes.
Copy after login

In this example, we successfully used the filesize() function to get the size of the file we created. Using this function helps us ensure that our website content is appropriately sized and does not impact the user experience.

Summary:

The filesize() function is a simple and effective way to get the file size in PHP. We can use this function to get the size of our files so that we can ensure that our website content is not too large and affects the user's experience.

The above is the detailed content of Get file size using filesize() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template