Skip to content

Instantly share code, notes, and snippets.

@wrossmann
Last active August 29, 2015 14:17
Show Gist options
  • Save wrossmann/fcb43a4e522d5da37783 to your computer and use it in GitHub Desktop.
Save wrossmann/fcb43a4e522d5da37783 to your computer and use it in GitHub Desktop.
Translate an integer representing unix permissions into human-readable format.
<?php
function int_to_perm($int) {
return sprintf(
'%s%s%s',
$int & 4 ? 'r' : '-', // read
$int & 2 ? 'w' : '-', // write
$int & 1 ? 'x' : '-' // execute
);
}
function int_to_extra($int) {
return sprintf(
'%s%s%s',
$int & 4 ? 'u' : '-', // setuid
$int & 2 ? 'g' : '-', // setgid
$int & 1 ? 's' : '-' // sticky
);
}
function int_to_perms($int) {
$o_part = $int & 7; // other
$g_part = $int >> 3 & 7; // group
$u_part = $int >> 6 & 7; // user
$x_part = $int >> 9 & 7; // extra
return sprintf(
"%s %s %s %s\n",
int_to_extra($x_part),
int_to_perm($u_part),
int_to_perm($g_part),
int_to_perm($o_part)
);
}
echo int_to_perms(4095); // ugs rwx rwx rwx
echo int_to_perms(18); // --- --- -w- -w-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment