Example 1 — Get the Size of a File
Write a short message to a file and print how many bytes it uses.
<?php
$file = __DIR__ . '/size_demo.txt';
file_put_contents($file, 'Hello, CodeToFun!');
$bytes = filesize($file);
echo "File size: $bytes bytes", PHP_EOL;
unlink($file); How It Works
- file_put_contents() writes the text Hello, CodeToFun! to the file.
- The text has 17 characters, and each plain letter uses one byte.
- filesize() reads the size from the file system and returns 17.
- echo prints the size inside the message.

