CFile::setPermissions PHP Method

setPermissions() public method

For UNIX systems.
public setPermissions ( string $permissions, boolean $recursive = False ) : CFile | boolean
$permissions string New filesystem object permissions in numeric (octal, i.e. '0755') format
$recursive boolean Apply permissions to directory contents flag.
return CFile | boolean Current CFile object on success, 'False' on fail.
    public function setPermissions($permissions, $recursive = False)
    {
        if ($this->getExists() && is_numeric($permissions)) {
            // '755' normalize to octal '0755'
            $perms_oct = octdec(str_pad($permissions, 4, '0', STR_PAD_LEFT));
            $success = @chmod($this->_realpath, $perms_oct);
            if ($success) {
                $this->_permissions = $permissions;
            }
            if ($success && $this->getIsDir() && $recursive) {
                $contents = $this->getContents(True);
                foreach ($contents as $filepath) {
                    if (!@chmod($filepath, $perms_oct)) {
                        $this->addLog('Unable to set permissions for "' . $filepath . '" to "' . $permissions . '"');
                        $success = False;
                    }
                }
            }
            if ($success) {
                return $this;
            }
        }
        $this->addLog('Unable to change permissions for filesystem object to "' . $permissions . '"');
        return False;
    }

Usage Example

Example #1
0
 public function testSetPermissions()
 {
     $filePath = CFile::createTemporary();
     CFile::setPermissions($filePath, "432");
     $this->assertTrue(CFile::permissions($filePath)->equals("432"));
     CFile::delete($filePath);
 }