JBZoo\Utils\FS::perms PHP Method

perms() public static method

Returns the file permissions as a nice string, like -rw-r--r-- or false if the file is not found.
public static perms ( string $file, integer $perms = null ) : string
$file string The name of the file to get permissions form
$perms integer Numerical value of permissions to display as text.
return string
    public static function perms($file, $perms = null)
    {
        if (null === $perms) {
            if (!file_exists($file)) {
                return false;
            }
            $perms = fileperms($file);
        }
        //@codeCoverageIgnoreStart
        if (($perms & 0xc000) == 0xc000) {
            // Socket
            $info = 's';
        } elseif (($perms & 0xa000) == 0xa000) {
            // Symbolic Link
            $info = 'l';
        } elseif (($perms & 0x8000) == 0x8000) {
            // Regular
            $info = '-';
        } elseif (($perms & 0x6000) == 0x6000) {
            // Block special
            $info = 'b';
        } elseif (($perms & 0x4000) == 0x4000) {
            // Directory
            $info = 'd';
        } elseif (($perms & 0x2000) == 0x2000) {
            // Character special
            $info = 'c';
        } elseif (($perms & 0x1000) == 0x1000) {
            // FIFO pipe
            $info = 'p';
        } else {
            // Unknown
            $info = 'u';
        }
        //@codeCoverageIgnoreEnd
        // Owner
        $info .= $perms & 0x100 ? 'r' : '-';
        $info .= $perms & 0x80 ? 'w' : '-';
        $info .= $perms & 0x40 ? $perms & 0x800 ? 's' : 'x' : ($perms & 0x800 ? 'S' : '-');
        // Group
        $info .= $perms & 0x20 ? 'r' : '-';
        $info .= $perms & 0x10 ? 'w' : '-';
        $info .= $perms & 0x8 ? $perms & 0x400 ? 's' : 'x' : ($perms & 0x400 ? 'S' : '-');
        // All
        $info .= $perms & 0x4 ? 'r' : '-';
        $info .= $perms & 0x2 ? 'w' : '-';
        $info .= $perms & 0x1 ? $perms & 0x200 ? 't' : 'x' : ($perms & 0x200 ? 'T' : '-');
        return $info;
    }

Usage Example

Example #1
0
 public function testWritable()
 {
     if (Sys::isWin()) {
         //skip('This functionality is not working on Windows.');
         return false;
     }
     if (Sys::isRoot()) {
         skip('These tests don\'t work when run as root');
     }
     isFalse(FS::writable('/no/such/file'));
     // Create a file to test with
     $dirname = dirname(__FILE__);
     $file = $dirname . '/test7';
     touch($file);
     chmod($file, 0644);
     // The file is owned by us so it should be writable
     isTrue(is_writable($file));
     is('-rw-r--r--', FS::perms($file));
     // Toggle writable bit off for us
     FS::writable($file, false);
     clearstatcache();
     isFalse(is_writable($file));
     is('-r--r--r--', FS::perms($file));
     // Toggle writable bit back on for us
     FS::writable($file, true);
     clearstatcache();
     isTrue(is_writable($file));
     is('-rw-r--r--', FS::perms($file));
     unlink($file);
 }