CFile::setOwner PHP Method

setOwner() public method

For POSIX systems. Asserts that user exists before process if posix_ functions are available.
public setOwner ( string | integer $owner, boolean $recursive = False ) : CFile | boolean
$owner string | integer New owner name or ID
$recursive boolean Apply owner to directory contents flag.
return CFile | boolean Current CFile object on success, 'False' on fail.
    public function setOwner($owner, $recursive = False)
    {
        if (function_exists('posix_getpwnam') && function_exists('posix_getpwuid')) {
            if (posix_getpwnam($owner) == False xor is_numeric($owner) && posix_getpwuid($owner) == False) {
                throw new CFileException('Unable to set owner for filesystem object. User "' . $owner . '" is not found.');
            }
        }
        if ($this->getExists()) {
            $success = @chown($this->_realpath, $owner);
            if ($success) {
                $this->_owner = $owner;
            }
            if ($success && $this->getIsDir() && $recursive) {
                $contents = $this->getContents(True);
                foreach ($contents as $filepath) {
                    if (!@chown($filepath, $owner)) {
                        $this->addLog('Unable to set owner for "' . $filepath . '" to "' . $owner . '"');
                        $success = False;
                    }
                }
            }
            if ($success) {
                return $this;
            }
        }
        $this->addLog('Unable to set owner for filesystem object to "' . $owner . '"');
        return False;
    }