Skip to content

Instantly share code, notes, and snippets.

@jesseschalken
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesseschalken/05f2123d4454c44ca62c to your computer and use it in GitHub Desktop.
Save jesseschalken/05f2123d4454c44ca62c to your computer and use it in GitHub Desktop.
A PHP class to generate Unix symbolic permission notation (eg "-rw-r--r--") from lstat()/fileperms()
<?php
class Perms {
/** @var int */
private $mode;
/** @param string $path */
function __construct($path) {
$this->mode = fileperms($path);
}
function symbolic() {
static $types = array(
015 => 'D',
014 => 's',
012 => 'l',
010 => '-',
006 => 'b',
004 => 'd',
002 => 'c',
001 => 'p',
);
return
$types[$this->bits(12, 4)] .
$this->perms(2, 'Ss') .
$this->perms(1, 'Ss') .
$this->perms(0, 'Tt');
}
private function perms($i, $s) {
$r = '-r';
$w = '-w';
$x = $this->bits($i + 9) ? $s : '-x';
return
$r[$this->bits($i * 3 + 2)] .
$w[$this->bits($i * 3 + 1)] .
$x[$this->bits($i * 3 + 0)];
}
private function bits($i, $n = 1) {
return ($this->mode >> $i) & ~(~0 << $n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment