filesize

filesize(string $filename): int|false

Returns the file size in bytes (false on failure); results are cached per path, so clearstatcache before re-reading.

C C23

C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void) {
    char path[] = "/tmp/fsz_XXXXXX"; // mkstemp fills in the XXXXXX part
    int fd = mkstemp(path);
    FILE *f = fdopen(fd, "wb");
    fwrite("abc123", 1, 6, f); // exactly 6 bytes
    fclose(f);

    struct stat st;
    stat(path, &st);
    printf("%lld bytes\n", (long long)st.st_size);

    // stat always reads the current size (no result cache to clear)
    f = fopen(path, "wb");
    fwrite("abc12345678", 1, 11, f);
    fclose(f);
    stat(path, &st);
    printf("%lld bytes\n", (long long)st.st_size);

    unlink(path);
    return 0;
}
Output
6 bytes
11 bytes

C++ C++26

C++26
#include <filesystem>
#include <fstream>
#include <iostream>

namespace fs = std::filesystem;

int main() {
    fs::path path = fs::temp_directory_path() / "fsz_cpp_demo.dat";
    std::ofstream(path, std::ios::binary) << "abc123"; // exactly 6 bytes
    // file_size reads the current directory entry directly (no cache)
    std::cout << fs::file_size(path) << " bytes\n";

    // overwrite with 11 bytes and re-check
    std::ofstream(path, std::ios::binary) << "abc12345678";
    std::cout << fs::file_size(path) << " bytes\n";

    fs::remove(path);
    return 0;
}
Output
6 bytes
11 bytes

C# v11.0.100

v11.0.100
using System.IO;

var path = Path.GetTempFileName();
File.WriteAllText(path, "abc123"); // exactly 6 bytes
// FileInfo.Length is read fresh each time a new FileInfo is created
Console.WriteLine($"{new FileInfo(path).Length} bytes");

// overwrite with 10 bytes and re-check
File.WriteAllText(path, "abc12345678");
Console.WriteLine($"{new FileInfo(path).Length} bytes");

File.Delete(path);
Output
6 bytes
11 bytes

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	path := fmt.Sprintf("%s/fsz_go_%d", os.TempDir(), os.Getpid())
	os.WriteFile(path, []byte("abc123"), 0644) // exactly 6 bytes
	fi, _ := os.Stat(path) // os.Stat always queries the filesystem fresh
	fmt.Println(fi.Size(), "bytes")

	// overwrite with 10 bytes and re-check
	os.WriteFile(path, []byte("abc12345678"), 0644)
	fi, _ = os.Stat(path)
	fmt.Println(fi.Size(), "bytes")

	os.Remove(path)
}
Output
6 bytes
11 bytes

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws Exception {
        Path path = Files.createTempFile("fsz", ".tmp");
        Files.writeString(path, "abc123"); // exactly 6 bytes
        // Files.size queries the filesystem fresh on each call
        System.out.println(Files.size(path) + " bytes");

        // overwrite with 10 bytes and re-check
        Files.writeString(path, "abc12345678");
        System.out.println(Files.size(path) + " bytes");

        Files.delete(path);
    }
}
Output
6 bytes
11 bytes

JavaScript v26.13.1

v26.13.1
const fs = require("fs");
const dir = fs.mkdtempSync(require("os").tmpdir() + "/fsz_");
const path = `${dir}/demo.dat`;

fs.writeFileSync(path, "abc123"); // exactly 6 bytes
// statSync queries the filesystem fresh on each call
console.log(`${fs.statSync(path).size} bytes`);

// overwrite with 10 bytes and re-check
fs.writeFileSync(path, "abc12345678");
console.log(`${fs.statSync(path).size} bytes`);

fs.rmSync(dir, { recursive: true, force: true }); // remove the private temp dir
Output
6 bytes
11 bytes

PHP v8.6.0

v8.6.0
$file = tempnam(sys_get_temp_dir(), 'fsz_');
file_put_contents($file, 'abc123'); // exactly 6 bytes
echo filesize($file), " bytes\n";

file_put_contents($file, 'abc12345678'); // exactly 11 bytes
// PHP refreshes the per-path stat cache on its own writes, so the new size
// shows up right away — clearstatcache() matters after EXTERNAL changes
clearstatcache();
echo filesize($file), " bytes\n";
unlink($file);
Output
6 bytes
11 bytes

Python v3.15.2

v3.15.2
import os
import tempfile

fd, path = tempfile.mkstemp(prefix="fsz_")
os.close(fd)
with open(path, "wb") as f:
    f.write(b"abc123")  # exactly 6 bytes

# os.path.getsize queries the filesystem fresh on each call
print(os.path.getsize(path), "bytes")

# overwrite with 10 bytes and re-check
with open(path, "wb") as f:
    f.write(b"abc12345678")
print(os.path.getsize(path), "bytes")

os.unlink(path)
Output
6 bytes
11 bytes

Rust v1.99.1

v1.99.1
use std::fs;

fn main() {
    let path = std::env::temp_dir().join(format!("fsz_rs_{}", std::process::id()));
    fs::write(&path, b"abc123").unwrap(); // exactly 6 bytes
    // metadata queries the filesystem fresh on each call
    println!("{} bytes", fs::metadata(&path).unwrap().len());

    // overwrite with 10 bytes and re-check
    fs::write(&path, b"abc12345678").unwrap();
    println!("{} bytes", fs::metadata(&path).unwrap().len());

    fs::remove_file(&path).unwrap();
}
Output
6 bytes
11 bytes

TypeScript v26.13.1

v26.13.1
import * as fs from "fs";
import * as os from "os";

const dir = fs.mkdtempSync(`${os.tmpdir()}/fsz_`);
const path = `${dir}/demo.dat`;

fs.writeFileSync(path, "abc123"); // exactly 6 bytes
// statSync queries the filesystem fresh on each call
console.log(`${fs.statSync(path).size} bytes`);

// overwrite with 10 bytes and re-check
fs.writeFileSync(path, "abc12345678");
console.log(`${fs.statSync(path).size} bytes`);

fs.rmSync(dir, { recursive: true, force: true }); // remove the private temp dir
Output
6 bytes
11 bytes

Language differences

C: the st_size field of stat is the byte count (off_t; cast to long long before printing for safety); stat performs a fresh syscall every time, with no result cache.

C++: std::filesystem::file_size (C++17) queries the directory entry directly, no cache.

C#: new FileInfo(path).Length — note you must construct a fresh FileInfo before reading Length; a stale instance may hold outdated data; File.ReadAllBytes(path).Length works but copies the file.

Go: os.Stat gives FileInfo.Size() (int64 bytes); every call queries fresh.

Java: Files.size(path) (NIO); the legacy File.length() also works.

JavaScript: fs.statSync(path).size (bytes); every call queries fresh.

PHP: filesize results are cached per path — the trap fires when an external process modifies the file (PHP's own file_put_contents refreshes the path's cache on write, so in-script write-then-read always sees the fresh value); in cross-process scenarios call clearstatcache() before re-reading with any stat-family function (filemtime/fileperms/filesize…); failure returns false (not an exception).

Python: os.path.getsize (bytes); every call queries fresh.

Rust: fs::metadata(path).len() (u64 bytes); every call queries fresh.

TypeScript: same as JS.