fileperms

fileperms(string $filename): int|false

Returns the file's full access mode (including file-type bits, e.g. octal 100644); ANDing with 0777 extracts the plain permission bits. False on failure.

C C23

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

int main(void) {
    char path[] = "/tmp/fpm_XXXXXX"; // mkstemp fills in the XXXXXX part
    int fd = mkstemp(path);
    close(fd);
    chmod(path, 0644);

    struct stat st;
    stat(path, &st);
    // st_mode packs file-type bits (0100000 octal = regular file) + permission bits
    printf("%o\n", (unsigned int)st.st_mode);        // full mode: 100644
    printf("%o\n", (unsigned int)st.st_mode & 0777); // permission bits only: 644

    chmod(path, 0755);
    stat(path, &st);
    printf("%o\n", (unsigned int)st.st_mode & 0777);

    unlink(path);
    return 0;
}
Output
100644
644
755

C++ C++26

C++26
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>
#include <unistd.h>

int main() {
    char path[] = "/tmp/fpm_cpp_XXXXXX"; // mkstemp fills in the XXXXXX part
    int fd = mkstemp(path);
    close(fd);
    chmod(path, 0644);

    struct stat st;
    stat(path, &st);
    // st_mode packs file-type bits (0100000 octal = regular file) + permission bits
    printf("%o\n", static_cast<unsigned>(st.st_mode));        // full mode: 100644
    printf("%o\n", static_cast<unsigned>(st.st_mode) & 0777); // permission bits: 644

    chmod(path, 0755);
    stat(path, &st);
    printf("%o\n", static_cast<unsigned>(st.st_mode) & 0777);

    unlink(path);
    return 0;
}
Output
100644
644
755

C# v11.0.100

v11.0.100
using System.IO;

var path = Path.GetTempFileName();
File.SetUnixFileMode(path,
    UnixFileMode.UserRead | UnixFileMode.UserWrite |
    UnixFileMode.GroupRead | UnixFileMode.OtherRead); // 0644

var mode = File.GetUnixFileMode(path);
// UnixFileMode is a flags enum WITHOUT file-type bits (those are Windows/
// POSIX concepts .NET hides); format as octal by converting to int
Console.WriteLine(Convert.ToString((int)mode, 8));      // 644
Console.WriteLine(mode);                                 // flags names
Console.WriteLine(mode.HasFlag(UnixFileMode.UserWrite)); // permission check

File.SetUnixFileMode(path,
    UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute |
    UnixFileMode.GroupRead | UnixFileMode.GroupExecute |
    UnixFileMode.OtherRead | UnixFileMode.OtherExecute); // 0755
Console.WriteLine(Convert.ToString((int)File.GetUnixFileMode(path), 8));
File.Delete(path);
Output
644
OtherRead, GroupRead, UserWrite, UserRead
True
755

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	path := fmt.Sprintf("%s/fpm_go_%d", os.TempDir(), os.Getpid())
	os.WriteFile(path, []byte("x"), 0644)
	os.Chmod(path, 0644)

	fi, _ := os.Stat(path)
	// os.FileMode is a cross-platform abstraction WITHOUT file-type bits;
	// .Perm() masks to the permission bits, .String() renders ls-style
	fmt.Printf("%o\n", fi.Mode().Perm()) // 644
	fmt.Println(fi.Mode())                // -rw-r--r--

	os.Chmod(path, 0755)
	fi, _ = os.Stat(path)
	fmt.Printf("%o\n", fi.Mode().Perm())

	os.Remove(path)
}
Output
644
-rw-r--r--
755

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws Exception {
        Path path = Files.createTempFile("fpm", ".tmp");
        Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rw-r--r--"));

        Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
        System.out.println(PosixFilePermissions.toString(perms)); // rw-r--r--
        // to octal: r=4 w=2 x=1 per triad, OR-ed into one int
        int mode = 0;
        for (PosixFilePermission p : perms) {
            int bit = switch (p) {
                case OWNER_READ -> 0400; case OWNER_WRITE -> 0200; case OWNER_EXECUTE -> 0100;
                case GROUP_READ -> 0040; case GROUP_WRITE -> 0020; case GROUP_EXECUTE -> 0010;
                case OTHERS_READ -> 0004; case OTHERS_WRITE -> 0002; case OTHERS_EXECUTE -> 0001;
            };
            mode |= bit;
        }
        System.out.println(Integer.toOctalString(mode)); // 644

        Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr-xr-x"));
        System.out.println(PosixFilePermissions.toString(Files.getPosixFilePermissions(path)));
        Files.delete(path);
    }
}
Output
rw-r--r--
644
rwxr-xr-x

JavaScript v26.13.1

v26.13.1
const fs = require("fs");
const os = require("os");
const dir = fs.mkdtempSync(os.tmpdir() + "/fpm_");
const path = `${dir}/demo.dat`;
fs.writeFileSync(path, "x"); // create the file first

fs.chmodSync(path, 0o644);

// statSync().mode carries file-type bits (0o100000 = regular file) too
const mode = fs.statSync(path).mode;
console.log(mode.toString(8)); // full mode: 100644
console.log((mode & 0o777).toString(8)); // permission bits only: 644

fs.chmodSync(path, 0o755);
console.log((fs.statSync(path).mode & 0o777).toString(8));

fs.unlinkSync(path);
fs.rmSync(dir, { recursive: true, force: true });
Output
100644
644
755

PHP v8.6.0

v8.6.0
$file = tempnam(sys_get_temp_dir(), 'fpm_');
chmod($file, 0644);

// fileperms returns the FULL mode: type bits + permission bits
// (0100000 octal prefix = regular file)
echo sprintf('%o', fileperms($file)), "\n";
echo sprintf('%o', fileperms($file) & 0777), "\n"; // mask off type bits: 644

chmod($file, 0755);
clearstatcache(); // stat-family results are cached per path
echo sprintf('%o', fileperms($file) & 0777), "\n";
unlink($file);
Output
100644
644
755

Python v3.15.2

v3.15.2
import os
import stat
import tempfile

fd, path = tempfile.mkstemp(prefix="fpm_")
os.close(fd)
os.chmod(path, 0o644)

mode = os.stat(path).st_mode
# st_mode packs file-type bits (0o100000 = regular file) + permission bits
print(oct(mode)[2:])                 # full mode: 100644
print(oct(stat.S_IMODE(mode))[2:])   # permission bits only: 644
print(stat.filemode(mode))           # ls-style string

os.chmod(path, 0o755)
print(oct(stat.S_IMODE(os.stat(path).st_mode))[2:])
os.unlink(path)
Output
100644
644
-rw-r--r--
755

Rust v1.99.1

v1.99.1
use std::fs;
use std::os::unix::fs::PermissionsExt; // std's unix-only permission extension

fn main() {
    let path = std::env::temp_dir().join(format!("fpm_rs_{}", std::process::id()));
    fs::write(&path, b"x").unwrap();
    fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();

    // .mode() returns the full unix mode: type bits + permission bits
    let mode = fs::metadata(&path).unwrap().permissions().mode();
    println!("{:o}", mode);        // full mode: 100644
    println!("{:o}", mode & 0o777); // permission bits only: 644
    println!("{:o}", fs::Permissions::from_mode(0o755).mode()); // 755

    fs::remove_file(&path).unwrap();
}
Output
100644
644
755

TypeScript v26.13.1

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

const dir = fs.mkdtempSync(`${os.tmpdir()}/fpm_`);
const path = `${dir}/demo.dat`;
fs.writeFileSync(path, "x"); // create the file first

fs.chmodSync(path, 0o644);

// statSync().mode carries file-type bits (0o100000 = regular file) too
const mode = fs.statSync(path).mode;
console.log(mode.toString(8)); // full mode: 100644
console.log((mode & 0o777).toString(8)); // permission bits only: 644

fs.chmodSync(path, 0o755);
console.log((fs.statSync(path).mode & 0o777).toString(8));

fs.unlinkSync(path);
fs.rmSync(dir, { recursive: true, force: true });
Output
100644
644
755

Language differences

C: the st_mode field of stat is the full mode; set with chmod(path, 0644); %o prints octal directly, & 0777 masks out the permission bits.

C++: same as C (std::filesystem::permissions can also set; reading is the same stat).

C#: File.GetUnixFileMode (.NET 7+, Unix platforms only) — UnixFileMode is a flags enum (UserRead/GroupWrite…, the members are User-prefixed rather than Owner) without file-type bits; the API does not exist on Windows (NTFS uses a completely different model), so cross-platform code branches on OperatingSystem.IsWindows().

Go: os.FileMode is a cross-platform abstraction — Perm() yields the plain permission bits, String() renders -rw-r--r--; the raw syscall mode (with type bits) hides in Sys() and is platform-specific — Go deliberately hides the type bits.

Java: NIO Files.getPosixFilePermissions (POSIX filesystems only; throws UnsupportedOperationException on Windows); PosixFilePermissions.toString gives rw-r--r--, and converting to octal means OR-ing r=4/w=2/x=1 yourself.

JavaScript: statSync().mode shares its origin with C's st_mode (type bits included); & 0o777 masks the permission bits; .toString(8) prints octal.

PHP: fileperms returns the full mode (type bits included; octal 100644 = regular file + rw-r--r--) — printing the decimal value directly yields the misleading 33188, so always sprintf('%o', ...) first; & 0777 extracts the plain permission bits; re-reading after chmod needs clearstatcache(); failure returns false.

Python: os.stat().st_mode includes type bits; stat.S_IMODE(mode) masks the permission bits and stat.filemode(mode) gives -rw-r--r-- directly; on Windows only the read-only bit (0o444/0o666-level) semantics exist — write/execute bits are meaningless.

Rust: std::os::unix::fs::PermissionsExt (std's built-in unix-only extension): from_mode/mode() convert to and from a raw octal mode, and .mode() includes type bits; the trait does not exist on Windows.

TypeScript: same as JS.