Example 1 — Show Permissions as Octal
Set a file to 0644 and read the permissions back in the usual format.
<?php
$file = __DIR__ . '/perm_demo.txt';
file_put_contents($file, 'data');
chmod($file, 0644);
$perms = fileperms($file);
echo 'Octal: ', substr(sprintf('%o', $perms), -4), PHP_EOL;
unlink($file); How It Works
- chmod() sets the file to 0644, which means the owner can read and write while everyone else can only read.
- fileperms() returns a big number that includes both the type and the permissions.
- sprintf('%o') prints that number in octal, and substr() keeps the last four digits.
- The result 0644 matches the value we set.

