echo filesize('test.htm');
Output is
130
$ar=scandir('.'); // current dir
while (list ($key, $val) = each ($ar)) {
echo $val . filesize($val). " bytes " ;
echo "<br>";
}
if (!file_exists('sample.txt')) {
echo 'File does not exist';
} else {
echo 'File size: ' . filesize('sample.txt') . ' bytes';
}
function format_size($size) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$unit_index = 0;
while ($size >= 1024 && $unit_index < count($units) - 1) {
$size /= 1024;
$unit_index++;
}
return round($size, 2) . ' ' . $units[$unit_index];
}
$file_size = filesize('sample.txt');
echo 'Formatted size: ' . format_size($file_size);
$dir = 'path/to/directory';
$files = scandir($dir);
foreach ($files as $file) {
if (is_file("$dir/$file")) {
echo "$file: " . filesize("$dir/$file") . " bytes
";
}
}
$url = 'http://example.com/sample.jpg';
$headers = get_headers($url, 1);
if (isset($headers['Content-Length'])) {
echo 'File size: ' . $headers['Content-Length'] . ' bytes';
} else {
echo 'Unable to determine file size.';
}
$file_path = '/var/www/html/sample.txt';
if (file_exists($file_path)) {
echo 'File size: ' . filesize($file_path) . ' bytes';
} else {
echo 'File does not exist';
}
$file_path = 'large_file.zip';
if (file_exists($file_path)) {
$file_size = filesize($file_path);
echo $file_size > 1_000_000 ? 'File size is greater than 1MB' : 'File size is less than 1MB';
} else {
echo 'File does not exist';
}
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.