You are here

public function FileSystem::chmod in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::chmod()

Sets the permissions on a file or directory.

This function will use the file_chmod_directory and file_chmod_file settings for the default modes for directories and uploaded/generated files. By default these will give everyone read access so that users accessing the files with a user account without the webserver group (e.g. via FTP) can read these files, and give group write permissions so webserver group members (e.g. a vhost account) can alter files uploaded and owned by the webserver.

PHP's chmod does not support stream wrappers so we use our wrapper implementation which interfaces with chmod() by default. Contrib wrappers may override this behavior in their implementations as needed.

Parameters

string $uri: A string containing a URI file, or directory path.

int $mode: Integer value for the permissions. Consult PHP chmod() documentation for more information.

Return value

bool TRUE for success, FALSE in the event of an error.

Overrides FileSystemInterface::chmod

3 calls to FileSystem::chmod()
FileSystem::copy in core/lib/Drupal/Core/File/FileSystem.php
Copies a file to a new location without invoking the file API.
FileSystem::move in core/lib/Drupal/Core/File/FileSystem.php
Moves a file to a new location without database changes or hook invocation.
FileSystem::prepareDirectory in core/lib/Drupal/Core/File/FileSystem.php
Checks that the directory exists and is writable.

File

core/lib/Drupal/Core/File/FileSystem.php, line 95

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function chmod($uri, $mode = NULL) {
  if (!isset($mode)) {
    if (is_dir($uri)) {
      $mode = $this->settings
        ->get('file_chmod_directory', static::CHMOD_DIRECTORY);
    }
    else {
      $mode = $this->settings
        ->get('file_chmod_file', static::CHMOD_FILE);
    }
  }
  if (@chmod($uri, $mode)) {
    return TRUE;
  }
  $this->logger
    ->error('The file permissions could not be set on %uri.', [
    '%uri' => $uri,
  ]);
  return FALSE;
}