AppserverIo\Appserver\Core\Utilities\FileSystem::chown PHP Method

chown() public static method

Chown function
public static chown ( string $path, integer $user, integer | null $group = null ) : boolean
$path string Relative or absolute path to a file or directory which should be processed.
$user integer The user that should gain owner rights.
$group integer | null The group that should gain group rights.
return boolean
    public static function chown($path, $user, $group = null)
    {
        // don't do anything under Windows
        if (FileSystem::getOsIdentifier() === self::OS_IDENTIFIER_WIN) {
            return;
        }
        // check if the path exists
        if (!file_exists($path)) {
            return false;
        }
        // change the owner
        if (chown($path, $user) === false) {
            throw new \Exception(sprintf('Can\'t change owner for directory/flie %s to %s', $path, $user));
        }
        // check if group is given too
        if (!is_null($group)) {
            if (chgrp($path, $group) === false) {
                throw new \Exception(sprintf('Can\'t change group for directory/flie %s to %s', $path, $group));
            }
        }
        return true;
    }

Usage Example

コード例 #1
0
 /**
  * Sets the configured user/group settings on the passed file.
  *
  * @param \SplFileInfo $fileInfo The file to set user/group for
  * @param string       $user     The user that has to own the passed file
  * @param string       $group    The group that has to own the passed file
  *
  * @return void
  */
 public function setUserRight(\SplFileInfo $fileInfo, $user = null, $group = null)
 {
     // Get our system configuration as it contains the user and group to set
     $systemConfiguration = $this->getInitialContext()->getSystemConfiguration();
     // Check for the existence of a user
     if ($user == null) {
         $user = $systemConfiguration->getParam('user');
     }
     // Check for the existence of a group
     if ($group == null) {
         $group = $systemConfiguration->getParam('group');
     }
     // change the owner for the passed file/directory
     FileSystem::chown($fileInfo, $user, $group);
 }
All Usage Examples Of AppserverIo\Appserver\Core\Utilities\FileSystem::chown